ListView with multiple row layout types - BaseAdapter does not work properly in Android

Hi, below is my class for baseadapter, but it does not work correctly:

private static class MyBaseAdapter extends BaseAdapter { private Context context; private LayoutInflater inflater; private MyBaseAdapter(Context context, FlipViewController controller) { inflater = LayoutInflater.from(context); this.context = context; } @Override public int getCount() { return Globals.list_album.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View layout = convertView; if (convertView == null ) { if (Globals.list_album.get(position).no_of_images == 1) { layout = inflater.inflate(R.layout.single_image_adapter, null); } else if(Globals.list_album.get(position).no_of_images == 2) { layout = inflater.inflate(R.layout.two_image_adapter, null); } else if(Globals.list_album.get(position).no_of_images == 3) { layout = inflater.inflate(R.layout.three_image_adapter, null); } else if(Globals.list_album.get(position).no_of_images == 4) { layout = inflater.inflate(R.layout.four_image_adapter, null); } else if(Globals.list_album.get(position).no_of_images == 5) { layout = inflater.inflate(R.layout.five_image_adapter, null); } } return layout; } } 

I want to load the layout according to the lack of images in Globals.list_album for each position. But it does not work properly. This does not work for no_of_images = 5 and 2, since I have such values ​​in the list. My values ​​for no_of_images are currently 4,3,1,2 and 5. So it should display the layouts four_image_adapter, three_image_adapter, one_image_adapter, two_image_adapter and five_image_adapter. But it displays four_image_adapter, three_image_adapter, one_image_adapter, four_image_adapter and three_image_adapter. All layouts have images depending on the number of images. Will someone tell me what should I do?

+7
android baseadapter getview
source share
1 answer

Since you need a different layout for different lines, use the methods below

getViewTypeCount () - returns information about the number of row types.

getItemViewType (int position) - returns information about which type of layout should be used depending on the position

using getItemViewType, you can determine the layout you need to use.

Like this

 public static final int TYPE_1 = 1; public static final int TYPE_2 = 2; public int getItemTypeCount(){ return 5; } public int getItemType(int position){ // Your if else code and return type ( TYPE_1 to TYPE_5 ) } public View getView(int position, View convertView, ViewGroup parent){ // Return the right kind of view based on getItemType(position) int type = getItemType(position); if(type == TYPE_1){ // create (or reuse) TYPE_1 view } else if() { }...... return myView; } 

Examples

http://android.amberfog.com/?p=296

http://www.survivingwithandroid.com/2014/08/android-listview-with-multiple-row.html

+8
source share

All Articles