I have an ArrayList with 11 objects, when put it into a Listview by extending a custom ArrayAdapter, it only shows 8 objects, from 9, 10 and 11 is duplicated 1, 2, 3 with content.
When I call System.out.println("Position: " + position);
with the int position from the SMS2PeopleAdapter class, it would only show 8 items with positions from 10, 9, 8, ... 3.
Could you help me solve this problem? Thanks.
Activity:
public class SMS2PeopleActivity extends AppCompatActivity {
ListView lv;
SMS2PeopleAdapter sms2PeopleAdapter;
ArrayList<SMS> listSMS2People;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms2people);
lv = (ListView) findViewById(R.id.list_view_messages);
listSMS2People = new ArrayList<>();
listSMS2People.add(new SMS("1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1"));
listSMS2People.add(new SMS("2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2"));
listSMS2People.add(new SMS("3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3"));
listSMS2People.add(new SMS("4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4"));
listSMS2People.add(new SMS("5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5"));
listSMS2People.add(new SMS("6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6"));
listSMS2People.add(new SMS("7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7"));
listSMS2People.add(new SMS("8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8"));
listSMS2People.add(new SMS("9", "9", "9", "9", "9", "9", "9", "9", "9", "9", "9", "9", "9", "9", "9", "9", "9"));
listSMS2People.add(new SMS("10", "10", "10", "10", "10", "10", "10", "10", "10", "10", "10", "10", "10", "10", "10", "10", "10"));
sms2PeopleAdapter = new SMS2PeopleAdapter(this, listSMS2People);
lv.setAdapter(sms2PeopleAdapter);
}
}
My custom ArrayAdapter:
public class SMS2PeopleAdapter extends ArrayAdapter<SMS> {
Activity activity;
public SMS2PeopleAdapter(Activity activity, ArrayList<SMS> products) {
super(activity, 0, products);
this.activity = activity;
}
public View getView(int position, View convertView, ViewGroup viewGroup) {
if (convertView == null) {
System.out.println("Position: " + position);
SMS sms = (SMS) getItem(position);
LayoutInflater inflater = activity.getLayoutInflater();
if (position % 2 == 0) {
convertView = inflater.inflate(R.layout.list_item_message_left, null);
TextView txtMsg = (TextView) convertView.findViewById(R.id.txtMsg);
txtMsg.setText(sms.getBody());
System.out.println(position + " Position: " + sms.getBody());
} else {
convertView = inflater.inflate(R.layout.list_item_message_right, null);
TextView txtMsg = (TextView) convertView.findViewById(R.id.txtMsg);
txtMsg.setText(sms.getBody());
System.out.println(position + " Position: " + sms.getBody());
}
}
return convertView;
}
}