title: How to populate a CustomListView with two different layouts for its items?
body: <blockquote>
    <p><strong>What my app should do</strong></p>
  </blockquote>
  
  <p>I would like to populate a ListView from two different tables (TABLE_GROUPS and TABLE_PASSWORDS) from SQLite database. </p>
  
  <p>First I display all groups one after another with the following custom item layout (custom_listview_single_item_group.xml). => This works fine!</p>
  
  <p>After the groups has been loaded to the same ListView I want to add all passwords to the lower part of my ListView with the following custom item layout (custom_listview_single_item_password.xml).</p>
  
  <blockquote>
    <p><strong>What my problem is</strong></p>
  </blockquote>
  
  <p>I do not know how I can change the single item layout for the passwords after all groups has been added to the listview with its own single item layout.</p>
  
  <blockquote>
    <p><strong>What my question is</strong></p>
  </blockquote>
  
  <p><strong><em>What is the best way to populate the listview with two different single item layouts?</em></strong></p>
  
  <blockquote>
    <p><strong>Visualized</strong></p>
  </blockquote>
  
  <p>Left: How it looks now</p>
  
  <p>Right: How it should look</p>
  
  <p><a href="https://i.stack.imgur.com/zNSif.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zNSif.png" alt="enter image description here"></a></p>
  
  <blockquote>
    <p><strong>Code - ShowItems.java</strong></p>
  </blockquote>
  
  <pre><code>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&lt;?&gt; listView, View itemView, int position, long id) {
              Log.v("Clicked position",""+ position);
          }
      });
  
      listShowItems.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
          @Override
          public boolean onItemLongClick(AdapterView&lt;?&gt; 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&lt;String&gt; getAllDataFromColumn(String tableName, int columnIndex) {
          ArrayList&lt;String&gt; strings = new ArrayList&lt;String&gt;();
          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></pre>
  
  <blockquote>
    <p><strong>Code - CustomLVAdapterShowItems</strong></p>
  </blockquote>
  
  <pre><code>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></pre>
  
  <blockquote>
    <p><strong>Code - activity_showitems.xml</strong></p>
  </blockquote>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;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"&gt;
  
  
      &lt;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"&gt;
  
          &lt;RelativeLayout
              android:id="@+id/toolBarTv"
              android:layout_width="match_parent"
              android:layout_height="match_parent"&gt;
  
  
              &lt;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" /&gt;
  
              &lt;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" /&gt;
  
              &lt;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" /&gt;
  
              &lt;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" /&gt;
  
              &lt;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" /&gt;
  
  
              &lt;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" /&gt;
  
              &lt;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" /&gt;
  
              &lt;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" /&gt;
  
          &lt;/RelativeLayout&gt;
  
  
      &lt;/android.support.v7.widget.Toolbar&gt;
  
      &lt;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"&gt;
  
          &lt;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" /&gt;
  
          &lt;RelativeLayout
              android:id="@+id/rLayoutScrollview"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"&gt;
  
              &lt;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" /&gt;
  
              &lt;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"&gt;
  
                  &lt;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"/&gt;
  
                  &lt;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"/&gt;
  
              &lt;/com.getbase.floatingactionbutton.FloatingActionsMenu&gt;
  
          &lt;/RelativeLayout&gt;
      &lt;/RelativeLayout&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <blockquote>
    <p><strong>Code - custom_listview_single_item_group.xml</strong></p>
  </blockquote>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;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"&gt;
  
  
      &lt;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" /&gt;
  
      &lt;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"&gt;
  
  
          &lt;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" /&gt;
  
          &lt;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" /&gt;
  
          &lt;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" /&gt;
      &lt;/RelativeLayout&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <blockquote>
    <p><strong>Code - custom_listview_single_item_password.xml</strong></p>
  </blockquote>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;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"&gt;
  
  
      &lt;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" /&gt;
  
      &lt;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"&gt;
  
          &lt;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" /&gt;
  
      &lt;/RelativeLayout&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>