我有一个 android.R.layout.simple_list_item_2 列表视图,它使用下面的代码,当我在列表视图中添加 10 个项目时一切正常,然后当我添加另一个项目时,列表的第一行不显示,(尽管ontextlistener 说它在那里)对于列表中第 10 项以外的每一项都会发生这种情况。
这是将项目添加到列表中的代码
meetingList.add(new String[] {db.get(0).getDate(), " "});
meetingList.add(new String[] {db.get(0).getName(), db.get(0).getEvent() });
for (int i = 1; i<db.size(); i++){
if (db.get(i).getDate() != database.list.get(i-1).getDate()){
meetingList.add(new String[] {db.get(i).getDate(), " "});
}
meetingList.add(new String[] { database.list.get(i).getName(), database.list.get(i).getEvent() });
}
这是我的列表适配器
setListAdapter(new ArrayAdapter<String[]>(this, android.R.layout.simple_list_item_2, android.R.id.text1, meetingList) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Must always return just a View.
final View view = super.getView(position, convertView, parent);
// If you look at the android.R.layout.simple_list_item_2 source, you'll see
// it's a TwoLineListItem with 2 TextViews - text1 and text2.
// TwoLineListItem listItem = (TwoLineListItem) view;
final String[] entry = meetingList.get(position);
TextView text1 = (TextView) view.findViewById(android.R.id.text1);
TextView text2 = (TextView) view.findViewById(android.R.id.text2);
text1.setText(entry[0]);
text2.setText(entry[1]);
if (entry[1].equals(" ")){
view.setBackgroundColor(Color.parseColor("#009ACD"));
text1.setTextColor(Color.parseColor("#FFFFFF"));
text1.setGravity(Gravity.CENTER);
text1.setTextSize(22);
}
else {
view.setBackgroundColor(Color.WHITE);
text1.setTextSize(25);
text2.setTextSize(18);
}
final Context context = getApplicationContext();
final int duration = Toast.LENGTH_SHORT;
text1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast toast = Toast.makeText(context, entry[0], duration);
toast.show();
}
});