【发布时间】:2016-05-21 06:17:43
【问题描述】:
我有一个包含 11 个对象的 ArrayList,当通过扩展自定义 ArrayAdapter 将其放入 Listview 时,它只显示 8 个对象,从 9、10 和 11 与内容重复 1、2、3。
当我使用 SMS2PeopleAdapter 类中的 int 位置调用 System.out.println("Position: " + position); 时,它只会显示位置为 10、9、8、... 3 的 8 个项目。
你能帮我解决这个问题吗? 谢谢。
活动:
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);
}
}
我的自定义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;
}
}
【问题讨论】:
标签: android android-arrayadapter