1
votes

Based on a few tutorials, I tried to do a simple android app that shows a list with strings. The problem was when I tried to introduce a new layout to the list items.

So you would have:

listItem1 -> layout1

listItem2 -> layout2

listItem3 -> layout1

etc

When I tried adding the new layout, my items became "scrambled" when scrolling. For example, it would display item1, item2, item3, item4... , and when scrolling down and coming back, my list would be: item40, item3, item20, item1 etc

Here's the code that I have:

(the activity_main.xml file has a LinearLayout > ListView)

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    items = new ArrayList<String>();
    for (int i = 0; i < 50; i++){
        items.add(i%2==0 ? "true" : "false");
    }

    ListView listView = (ListView) findViewById(R.id.ListViewId);
    listView.setAdapter(new ListItemAdapter(this, android.R.layout.simple_list_item_1, items));

}

(list_item_1 and list_item_2 are layouts with a textview)

public class ListItemAdapter extends ArrayAdapter<String> {
private ArrayList<String> items;
private LayoutInflater mInflater;

public ListItemAdapter(Context context, int textViewResourceId,
        ArrayList<String> items) {
    super(context, textViewResourceId, items);
    this.items = items;
    mInflater = (LayoutInflater)this.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    int type = items.get(position).equals("true") ? 0 : 1;
    if (convertView == null) {
        switch(type){
        case 1:
            convertView = mInflater.inflate(R.layout.list_item_1, null);
            break;
        case 0:
            convertView = mInflater.inflate(R.layout.list_item_2, null);
            break;
        }

    }
    String item = items.get(position);
    if (item != null) {
        switch(type){
        case 1:
            TextView text = (TextView) convertView.findViewById(R.id.day);
            if (text != null) {
                text.setText(item);
            }           
            break;
        case 0:
            TextView day = (TextView) convertView.findViewById(R.id.text);

            if (day != null) {
                day.setText(item);
            }
            break;
        }

    }

    return convertView;
}
}

As you can see, depending on the item (true or false), I change the layout.

Any input would be really appreciated!

Btw, this is a simplified example of what I'm trying to do...

1
instead of this you can use same layouts and Visibility in view. it is simple for this type of design. if you need them then make it visible otherwise make it invisibleDhaval Parmar

1 Answers

1
votes

Please override getViewTypeCount() and getItemViewType(int) methods too. It should work. Also please go through this link Android ListView with different layouts for each row.