mardi 21 mars 2017

The position of the items in my SwipeMenuListView is random after scrolling

The position of the items in SwipeMenuListView changes randomly after scrolling. The items in the listview are also not visible like only items upto Item 5 is displayed in the Listview. Here is my code:

public ArrayList<Item> initArrayList() {
    ArrayList<Item> items = new ArrayList<>();
    items.add(new Item("Item 1", "First Item on the list", "abc", "Item 1.4", "Item 1.5"));
    items.add(new Item("Item 2", "Second Item on the list", "xyz", "item 2.4", "item 2.5"));
    items.add(new Item("Item 3", "Third Item on the list", "qwe", "item 3.4", "item 3.5"));
    items.add(new Item("Item 4", "Forth Item on the list", "qwe", "item 4.4", "item 4.5"));
    items.add(new Item("Item 5", "Fifth Item on the list", "qwe", "item 5.4", "item 5.5"));
    items.add(new Item("Item 6", "Sixth Item on the list", "qwe", "item 6.4", "item 6.5"));
    items.add(new Item("Item 7", "Seventh Item on the list", "qwe", "item 7.4", "item 7.5"));
    items.add(new Item("Item 8", "Eight Item on the list", "qwe", "item 8.4", "item 8.5"));
    return items;
}

Here is my DataAdapter and Item class:

public class DataAdapter extends ArrayAdapter<Item> {

private final Context context;
private final ArrayList<Item> itemsArrayList;

public DataAdapter(Context context, ArrayList<Item> itemsArrayList) {

    super(context, R.layout.list_item2, itemsArrayList);

    this.context = context;
    this.itemsArrayList = itemsArrayList;
}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // 1. Create inflater
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    // 2. Get rowView from inflater
    View rowView = inflater.inflate(R.layout.list_item2, parent, false);

    // 3. Get the two text view from the rowView
    TextView tv1 = (TextView) rowView.findViewById(R.id.tv1);
    TextView tv2 = (TextView) rowView.findViewById(R.id.tv2);
    TextView tv3 = (TextView) rowView.findViewById(R.id.tv3);
    TextView tv4 = (TextView) rowView.findViewById(R.id.tv4);
    TextView tv5 = (TextView) rowView.findViewById(R.id.tv5);

    // 4. Set the text for textView
    tv1.setText(itemsArrayList.get(position).getTv1());
    tv2.setText(itemsArrayList.get(position).getTv2());
    tv3.setText(itemsArrayList.get(position).getTv3());
    tv4.setText(itemsArrayList.get(position).getTv4());
    tv5.setText(itemsArrayList.get(position).getTv5());

    // 5. retrn rowView
    return rowView;
}

Here is mu Item class:

public class Item {

private String tv1;
private String tv2;
private String tv3;
private String tv4;
private String tv5;

public Item(String tv1, String tv2, String tv3,String tv4, String tv5) {
    super();
    this.tv1 = tv1;
    this.tv2 = tv2;
    this.tv3 = tv3;
    this.tv4 = tv4;
    this.tv5 = tv5;
}

public String getTv1(){
    return tv1;
}

public String getTv2(){
    return tv2;
}

public String getTv3(){
    return tv3;
}

public String getTv4(){
    return tv4;
}

public String getTv5(){
    return tv5;
}




Aucun commentaire:

Enregistrer un commentaire