我的自定义适配器中扩展ArrayAdapter的getView()方法被调用多次使用相同的位置值。我尝试了自定义适配器,当我提供的ArrayList有5个项目时它工作,当ArrayList有3个项目时,它没有按预期工作。
这是代码
public class ShowSchedule extends Activity {
private DBHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_courses);
db = new DBHelper(this);
String stdID = HomePage.STD_ID;
ListView mainList = (ListView) findViewById(R.id.list_courses);
ArrayList<Entry> entries = new ArrayList<Entry>();
Cursor cursor = db.executeQuery("SELECT * FROM " + DBHelper.TIME_COURSE_TABLE_NAME + " WHERE " +
DBHelper.TIME_COURSE_COLUMN_COURSE_ID + " IN (SELECT " + DBHelper.STD_COURSE_COLUMN_COURSEID +
" FROM " + DBHelper.STD_COURSE_TABLE_NAME + " WHERE " + DBHelper.STD_COURSE_COLUMN_STDID +
" = " + stdID + ")");
String courseID = "", timeID = "", day = "";
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
courseID = cursor.getString(cursor.getColumnIndex(DBHelper.TIME_COURSE_COLUMN_COURSE_ID));
Cursor temp = db.executeQuery("SELECT " + DBHelper.COURSE_COLUMN_NAME + " FROM " + DBHelper.COURSE_TABLE_NAME
+ " WHERE " + DBHelper.COURSE_COLUMN_ID + " = " + courseID);
temp.moveToFirst();
String courseName = temp.getString(temp.getColumnIndex(DBHelper.COURSE_COLUMN_NAME));
temp = db.executeQuery("SELECT " + DBHelper.ROOM_COURSE_COLUMN_ROOM_ID + " FROM " +
DBHelper.ROOM_COURSE_TABLE_NAME + " WHERE " + DBHelper.ROOM_COURSE_COLUMN_COURSE_ID + " = " + courseID);
temp.moveToFirst();
String room = temp.getString(temp.getColumnIndex(DBHelper.ROOM_COURSE_COLUMN_ROOM_ID));
timeID = cursor.getString(cursor.getColumnIndex(DBHelper.TIME_COURSE_COLUMN_TIME_ID));
day = cursor.getString(cursor.getColumnIndex(DBHelper.TIME_COURSE_COLUMN_DAY));
entries.add(new Entry(courseID, courseName, timeID, day, room));
cursor.moveToNext();
}
CoursesAdapter adapter = new CoursesAdapter(this, entries);
mainList.setAdapter(adapter);
}
}
和适配器
public class CoursesAdapter extends ArrayAdapter<Entry> {
private final Context context;
private final ArrayList<Entry> entries;
public CoursesAdapter(Context context, ArrayList<Entry> objects) {
super(context, R.layout.activity_list_item, objects);
this.context = context;
this.entries = objects;
}
private final String[] days = { "Sun", "Mon", "Tue", "Wed", "Thu" };
@Override
public int getCount() {
return entries.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflator.inflate(R.layout.activity_list_item, parent, false);
int[] id = {
R.id.tvCourseName1,
R.id.tvCourseName2,
R.id.tvCourseName3,
R.id.tvCourseName4,
R.id.tvCourseName5,
R.id.tvCourseName6
};
TextView tvDay = (TextView) view.findViewById(R.id.txDay);
tvDay.setText(days[position]);
for (Entry e : entries) {
if (e.getDay().equals(days[position])) {
TextView tv = (TextView) view.findViewById(id[Integer.parseInt(e.getTimeID()) - 1]);
tv.setText(e.getInfo());
}
}
return view;
}
}