android - 如何为其项目填充具有两种不同布局的 CustomListView?

标签 android sqlite listview android-adapter populate

What my app should do

我想从 SQLite 数据库的两个不同表(TABLE_GROUPS 和 TABLE_PASSWORDS)填充一个 ListView。

首先,我使用以下自定义项目布局 (custom_listview_single_item_group.xml) 一个接一个地显示所有组。 => 这很好用!

将组加载到同一个 ListView 后,我想使用以下自定义项目布局 (custom_listview_single_item_password.xml) 将所有密码添加到我的 ListView 的下部。

What my problem is

在将所有组添加到具有自己的单项布局的 ListView 后,我不知道如何更改密码的单项布局。

What my question is

用两种不同的单项布局填充 ListView 的最佳方法是什么?

Visualized

左:现在的样子

正确:它应该是什么样子

enter image description here

Code - ShowItems.java

public void showItemsListView(){

    GridView gridShowItems = (GridView)findViewById(R.id.gridShowItems);
    ListView listShowItems = (ListView) findViewById(R.id.listItems);

    //getAllGroupNamesFromDB(columnIndex)
    String[] strArrGroupNames = new String[getAllDataFromColumn("GROUPS",1).size()];
    String[] strArrGroupImageNames = new String[getAllDataFromColumn("GROUPS",4).size()];
    strArrGroupNames = getAllDataFromColumn("GROUPS",1).toArray(strArrGroupNames);
    strArrGroupImageNames = getAllDataFromColumn("GROUPS",4).toArray(strArrGroupImageNames);

    listShowItems.setVisibility(View.VISIBLE);
    gridShowItems.setVisibility(View.GONE);

    listShowItems.setAdapter(new CustomLVAdapterShowItems(this, strArrGroupNames, strArrGroupImageNames));

    listShowItems.setLongClickable(true);

    listShowItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> listView, View itemView, int position, long id) {
            Log.v("Clicked position",""+ position);
        }
    });

    listShowItems.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position, long id) {

            //Set Edit-Options to GONE and display default titlebar
            setTitleBarMode(2);

            if (itemsRow != null) {
                itemsRow.setBackgroundResource(R.color.color_blue_listviewgridview);
            }

            //Set background color to selected item
            itemsRow = arg1;
            itemsRow.setBackgroundColor(Color.GREEN);

            TextView txtGroupName = (TextView)itemsRow.findViewById(R.id.txtTypeName);

            //Save itemName to sharedPreferences for editoptions
            selectedIconPrefs = getApplicationContext().getSharedPreferences("selectedIconPrefs", MODE_PRIVATE);
            SharedPreferences.Editor editor = selectedIconPrefs.edit();
            editor.putString("selectedIconPrefs", txtGroupName.getText().toString().trim());
            editor.commit();

            return true;
        }
    });
}


public ArrayList<String> getAllDataFromColumn(String tableName, int columnIndex) {
        ArrayList<String> strings = new ArrayList<String>();
        String query = String.format("SELECT * FROM "+tableName);
        Cursor c = db.getReadableDatabase().rawQuery(query, null);

        if (c.moveToFirst())
            do {
                strings.add(c.getString(columnIndex));
            } while (c.moveToNext());

        return strings;
    }

Code - CustomLVAdapterShowItems

public class CustomLVAdapterShowItems extends BaseAdapter {

    String [] strItemNames;
    Context context;
    String [] strImageNames;
    private static LayoutInflater inflater=null;

    public CustomLVAdapterShowItems(Context contextshowgroups, String[] listGroupNames, String[] listGroupImages) {

        strItemNames=listGroupNames;
        context=contextshowgroups;
        strImageNames=listGroupImages;
        inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return strItemNames.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public class Holder{
        TextView txtViewItemName;
        ImageView imgItem;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Holder holder=new Holder();
        View rowView;
        rowView = inflater.inflate(R.layout.custom_listview_single_item_group, null);
        holder.txtViewItemName =(TextView) rowView.findViewById(R.id.txtTypeName);
        holder.imgItem =(ImageView) rowView.findViewById(R.id.imgTypeIcon);
        holder.txtViewItemName.setText(strItemNames[position]);
        holder.imgItem.setImageBitmap(BitmapFactory.decodeFile(strImageNames[position]));

        // load image
        try {
            Drawable d = Drawable.createFromStream(context.getAssets().open(strImageNames[position]+".png"), null);
            holder.imgItem.setBackground(d);
        }

        catch(IOException ex) {
            Log.d("IOException: ", ""+ex);
            return null;
        }

        return rowView;
    }
}

Code - activity_showitems.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="spicysoftware.com.passremember.ShowItems">


    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#095C9B"
        app:contentInsetLeft="0dp"
        app:contentInsetStart="0dp"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

        <RelativeLayout
            android:id="@+id/toolBarTv"
            android:layout_width="match_parent"
            android:layout_height="match_parent">


            <ImageView
                android:id="@+id/imgBack"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_centerVertical="true"
                android:layout_marginLeft="16dp"
                android:layout_toEndOf="@+id/editTextSearch"
                android:background="@drawable/ic_arrow_back_white_48dp"
                android:visibility="gone"
                fab:srcCompat="@drawable/ic_arrow_back_white_48dp" />

            <TextView
                android:id="@+id/txtAppName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="16dp"
                android:fontFamily="sans-serif-smallcaps"
                android:text="@string/app_name"
                android:textColor="@android:color/white"
                android:textSize="20sp"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/editTextSearch"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="16dp"
                android:layout_toStartOf="@+id/imgSearch"
                android:fontFamily="sans-serif-smallcaps"
                android:hint="Search..."
                android:inputType="text"
                android:singleLine="true"
                android:textColor="@android:color/white"
                android:textSize="16sp"
                android:visibility="gone" />

            <ImageView
                android:id="@+id/imgTiles"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_alignTop="@+id/imgMenu"
                android:layout_marginLeft="16dp"
                android:layout_toStartOf="@+id/imgMenu"
                android:background="@drawable/appswhite" />

            <ImageView
                android:id="@+id/imgEdit"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_alignTop="@+id/imgMenu"
                android:layout_marginLeft="16dp"
                android:layout_toStartOf="@+id/imgMenu"
                android:background="@drawable/ic_edit_white_48dp"
                android:visibility="gone" />


            <ImageView
                android:id="@+id/imgMenu"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_alignParentEnd="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:background="@drawable/ic_more_vert_white_48dp" />

            <ImageView
                android:id="@+id/imgDelete"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_alignParentEnd="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:background="@drawable/ic_delete_white_48dp"
                android:visibility="gone" />

            <ImageView
                android:id="@+id/imgSearch"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_centerVertical="true"
                android:layout_toStartOf="@+id/imgTiles"
                android:background="@drawable/ic_search_white_48dp" />

        </RelativeLayout>


    </android.support.v7.widget.Toolbar>

    <RelativeLayout
        android:id="@+id/toolBarTvTwo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_below="@+id/toolbar">

        <GridView
            android:id="@+id/gridShowItems"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginTop="16dp"
            android:background="@android:color/transparent"
            android:horizontalSpacing="6dp"
            android:numColumns="3"
            android:clickable="true"
            android:verticalSpacing="6dp"
            android:visibility="gone" />

        <RelativeLayout
            android:id="@+id/rLayoutScrollview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ListView
                android:id="@+id/listItems"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginTop="16dp"
                android:background="@android:color/transparent"
                android:clickable="true"
                android:dividerHeight="8dp" />

            <com.getbase.floatingactionbutton.FloatingActionsMenu
                android:id="@+id/multiple_actions"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                fab:fab_addButtonColorNormal="#095C9B"
                fab:fab_addButtonColorPressed="@color/white_pressed"
                fab:fab_addButtonPlusIconColor="@color/half_black"
                fab:fab_labelStyle="@style/menu_labels_style"
                android:layout_marginBottom="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginEnd="16dp">

                <com.getbase.floatingactionbutton.FloatingActionButton
                    android:id="@+id/flbtnNewGroup"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    fab:fab_icon="@drawable/ic_folder_open_white_48dp"
                    fab:fab_colorNormal="#095C9B"
                    fab:fab_title="@string/group"
                    fab:fab_colorPressed="@color/white_pressed"/>

                <com.getbase.floatingactionbutton.FloatingActionButton
                    android:id="@+id/flbtnNewPassword"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    fab:fab_icon="@drawable/ic_description_white_48dp"
                    fab:fab_colorNormal="#095C9B"
                    fab:fab_title="@string/strpassword"
                    fab:fab_colorPressed="@color/white_pressed"/>

            </com.getbase.floatingactionbutton.FloatingActionsMenu>

        </RelativeLayout>
    </RelativeLayout>
</RelativeLayout>

Code - custom_listview_single_item_group.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/customlistviewitem"
    android:elevation="1dp"
    android:orientation="horizontal">


    <ImageView
        android:id="@+id/imgTypeIcon"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_gravity="center"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="4dp"
        android:background="@drawable/mail"
        android:scaleType="fitXY" />

    <RelativeLayout
        android:id="@+id/rLayText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="16dp"
        android:layout_toEndOf="@+id/imgTypeIcon">


        <ImageView
            android:id="@+id/imgArrowRight"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:layout_alignParentEnd="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="16dp"
            android:background="@drawable/arrowright" />

        <TextView
            android:id="@+id/txtTypeName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:fontFamily="sans-serif-smallcaps"
            android:text="Gruppe"
            android:textColor="@android:color/white"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/txtGroupCountFiles"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/txtTypeName"
            android:fontFamily="sans-serif-smallcaps"
            android:text="0 Entries"
            android:textColor="@android:color/white"
            android:textSize="12sp" />
    </RelativeLayout>

</RelativeLayout>

Code - custom_listview_single_item_password.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/customlistviewitem"
    android:elevation="1dp"
    android:orientation="horizontal">


    <ImageView
        android:id="@+id/imgTypeIcon"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_gravity="center"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="4dp"
        android:background="@drawable/mail"
        android:scaleType="fitXY" />

    <RelativeLayout
        android:id="@+id/rLayText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="16dp"
        android:layout_toEndOf="@+id/imgTypeIcon">

        <TextView
            android:id="@+id/txtTypeName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:fontFamily="sans-serif-smallcaps"
            android:text="Gruppe"
            android:textColor="@android:color/white"
            android:textSize="18sp"
            android:textStyle="bold" />

    </RelativeLayout>

</RelativeLayout>

最佳答案

如果您升级到 RecyclerView,您会发现生活更轻松,如果您要这样做,这里有一个很好的答案: https://stackoverflow.com/a/26245463/3009199

如果你想坚持使用 ListView,这里有一个教程: http://android.amberfog.com/?p=296

他介绍的时候你要看

 @Override
    public int getItemViewType(int position) {

然后你的适配器会这样做:

switch (type) {
                case TYPE_ITEM:
                    convertView = mInflater.inflate(R.layout.item1, null);
                    break;
                case TYPE_SEPARATOR:
                    convertView = mInflater.inflate(R.layout.item2, null);
                    break;

简而言之,唯一复杂的是让代码知道哪一行应该是哪一行。这可以像 position > 4 或其他任何东西一样简单。如果它更复杂,您可能希望将适配器更改为使用对象而不是简单的字符串,并且每个对象都可以告诉适配器它的行类型。

关于android - 如何为其项目填充具有两种不同布局的 CustomListView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45208938/

相关文章:

Android 复选框文本合并到复选框

sqlite - SQLite:条件DDL语句

java - 如何在android中使用对象模型数据库获取日期和月份列

c# - 滚动时 Xamarin.Android 中的 ListView 内存不足

ios - 在 react native 中居中对齐 ListView 的内容

android - 如何在 Windows 中通过 flutter 测试 ios 应用

android - java.lang.SecurityException : Unknown calling package name, com.google.android.gms.common.internal.zzs

database - Actionscript-3 类 Hibernate 框架

java - Android 中索引 3 无效,大小为 0

java - startSearch 不启动搜索 Activity