In my custom ListAdapter, the first time GetView () is called, the transformation is converted to NULL, but the second time it is passed as the view that was created the first time. My ListView has 4 rows, and all 4 are simultaneously displayed on the screen. From the documentation, it seems that convertView should be a view that has already been created and is now scrolling from the screen. I expected convertView to be null only 4 times, so that it will create / inflate 4 separate views. Should I have a convertView after the first getView call? Thank.
In OnCreate ():
Cursor questions = db.loadQuestions(b.getLong("categoryId"), inputLanguage.getLanguageId(), outputLanguage.getLanguageId());
startManagingCursor(questions);
ListAdapter adapter = new QuestionsListAdapter(this, questions);
ListView list = (ListView)findViewById(R.id.list1);
setListAdapter(adapter);
Adapter class
private class QuestionsListAdapter extends BaseAdapter implements ListAdapter{
private Cursor c;
private Context context;
public QuestionsListAdapter(Context context, Cursor c) {
this.c = c;
this.context = context;
}
public Object getItem(int position) {
c.moveToPosition(position);
return new Question(c);
}
public long getItemId(int position) {
c.moveToPosition(position);
return new Question(c).get_id();
}
@Override
public int getItemViewType(int position) {
Question currentQuestion = (Question)this.getItem(position);
if (currentQuestion.getType().equalsIgnoreCase("text"))
return 0;
else if (currentQuestion.getType().equalsIgnoreCase("range"))
return 0;
else if (currentQuestion.getType().equalsIgnoreCase("yesNo"))
return 2;
else if (currentQuestion.getType().equalsIgnoreCase("picker"))
return 0;
else if (currentQuestion.getType().equalsIgnoreCase("command"))
return 0;
else if (currentQuestion.getType().equalsIgnoreCase("datePicker"))
return 0;
else if (currentQuestion.getType().equalsIgnoreCase("diagram"))
return 0;
else
return -1;
}
@Override
public int getViewTypeCount() {
return 7;
}
public int getCount() {
return c.getCount();
}
public View getView(int position, View convertView, ViewGroup viewGroup) {
Question currentQuestion = (Question)this.getItem(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.question_row_text, null);
}
return convertView;
}
}
source
share