Android ListView 和带有 ViewHolder 的自定义适配器
Posted
技术标签:
【中文标题】Android ListView 和带有 ViewHolder 的自定义适配器【英文标题】:Android ListView and custom adapter with ViewHolder 【发布时间】:2015-08-22 04:07:41 【问题描述】:我的 ListView 适配器在我的 ArrayList 中显示项目时遇到问题...
在大多数情况下,它似乎工作......每个项目的视图都是正确的......但是,当我实现 (convertView == null)...... 时,验证似乎搞砸了......
如果没有这个...显示器在所有正确的验证下都可以完美运行...但是一旦我输入 (convertView == null) 部分,我的验证都无法正常工作...这是最奇怪的事情.
完美运行...
public View getView(int position, View convertView, ViewGroup parent)
// View rowView = convertView;
final ViewHolder viewHolder;
LayoutInflater theInflater = LayoutInflater.from(context);
convertView = theInflater.inflate(R.layout.score_layout, null);
viewHolder = new ViewHolder();
viewHolder.textViewScoreQuestion = (TextView) convertView.findViewById(R.id.textViewScoreQuestion);
viewHolder.checkBoxScoreOne = (CheckBox) convertView.findViewById(R.id.checkBoxScoreOne);
viewHolder.checkBoxScoreTwo = (CheckBox) convertView.findViewById(R.id.checkBoxScoreTwo);
viewHolder.checkBoxScoreThree = (CheckBox) convertView.findViewById(R.id.checkBoxScoreThree);
viewHolder.checkBoxScoreFour = (CheckBox) convertView.findViewById(R.id.checkBoxScoreFour);
Questions question = questionsInOrder.get(position);
String givenAnswer = answerChoices.get(position);
viewHolder.textViewScoreQuestion.setText(question.getQuestion());
viewHolder.checkBoxScoreOne.setText(question.getAnswer1());
viewHolder.checkBoxScoreTwo.setText(question.getAnswer2());
viewHolder.checkBoxScoreThree.setText(question.getAnswer3());
viewHolder.checkBoxScoreFour.setText(question.getAnswer4());
// display incorrect answers as red, and correct answers as blue
if (givenAnswer.equals(question.getAnswer1()) && !(givenAnswer.equals(question.getCorrect())))
viewHolder.checkBoxScoreOne.setTextColor(context.getResources().getColor(R.color.red));
if (givenAnswer.equals(question.getAnswer1()) && (givenAnswer.equals(question.getCorrect())))
viewHolder.checkBoxScoreOne.setTextColor(context.getResources().getColor(R.color.holo_blue_dark));
if (givenAnswer.equals(question.getAnswer2()) && !(givenAnswer.equals(question.getCorrect())))
viewHolder.checkBoxScoreTwo.setTextColor(context.getResources().getColor(R.color.red));
if (givenAnswer.equals(question.getAnswer2()) && (givenAnswer.equals(question.getCorrect())))
viewHolder.checkBoxScoreTwo.setTextColor(context.getResources().getColor(R.color.holo_blue_dark));
if (givenAnswer.equals(question.getAnswer3()) && !(givenAnswer.equals(question.getCorrect())))
viewHolder.checkBoxScoreThree.setTextColor(context.getResources().getColor(R.color.red));
if (givenAnswer.equals(question.getAnswer3()) && (givenAnswer.equals(question.getCorrect())))
viewHolder.checkBoxScoreThree.setTextColor(context.getResources().getColor(R.color.holo_blue_dark));
if (givenAnswer.equals(question.getAnswer4()) && !(givenAnswer.equals(question.getCorrect())))
viewHolder.checkBoxScoreFour.setTextColor(context.getResources().getColor(R.color.red));
if (givenAnswer.equals(question.getAnswer4()) && (givenAnswer.equals(question.getCorrect())))
viewHolder.checkBoxScoreFour.setTextColor(context.getResources().getColor(R.color.holo_blue_dark));
// check all of the correct answers
if (question.getAnswer1().equals(question.getCorrect()))
viewHolder.checkBoxScoreOne.setChecked(true);
if (question.getAnswer2().equals(question.getCorrect()))
viewHolder.checkBoxScoreTwo.setChecked(true);
if (question.getAnswer3().equals(question.getCorrect()))
viewHolder.checkBoxScoreThree.setChecked(true);
if (question.getAnswer4().equals(question.getCorrect()))
viewHolder.checkBoxScoreFour.setChecked(true);
return convertView;
验证搞砸了:
public View getView(int position, View convertView, ViewGroup parent)
// View rowView = convertView;
final ViewHolder viewHolder;
if (convertView == null)
LayoutInflater theInflater = LayoutInflater.from(context);
convertView = theInflater.inflate(R.layout.score_layout, null);
viewHolder = new ViewHolder();
viewHolder.textViewScoreQuestion = (TextView) convertView.findViewById(R.id.textViewScoreQuestion);
viewHolder.checkBoxScoreOne = (CheckBox) convertView.findViewById(R.id.checkBoxScoreOne);
viewHolder.checkBoxScoreTwo = (CheckBox) convertView.findViewById(R.id.checkBoxScoreTwo);
viewHolder.checkBoxScoreThree = (CheckBox) convertView.findViewById(R.id.checkBoxScoreThree);
viewHolder.checkBoxScoreFour = (CheckBox) convertView.findViewById(R.id.checkBoxScoreFour);
convertView.setTag(viewHolder);
else
viewHolder = (ViewHolder) convertView.getTag();
Questions question = questionsInOrder.get(position);
String givenAnswer = answerChoices.get(position);
viewHolder.textViewScoreQuestion.setText(question.getQuestion());
viewHolder.checkBoxScoreOne.setText(question.getAnswer1());
viewHolder.checkBoxScoreTwo.setText(question.getAnswer2());
viewHolder.checkBoxScoreThree.setText(question.getAnswer3());
viewHolder.checkBoxScoreFour.setText(question.getAnswer4());
// display incorrect answers as red, and correct answers as blue
if (givenAnswer.equals(question.getAnswer1()) && !(givenAnswer.equals(question.getCorrect())))
viewHolder.checkBoxScoreOne.setTextColor(context.getResources().getColor(R.color.red));
if (givenAnswer.equals(question.getAnswer1()) && (givenAnswer.equals(question.getCorrect())))
viewHolder.checkBoxScoreOne.setTextColor(context.getResources().getColor(R.color.holo_blue_dark));
if (givenAnswer.equals(question.getAnswer2()) && !(givenAnswer.equals(question.getCorrect())))
viewHolder.checkBoxScoreTwo.setTextColor(context.getResources().getColor(R.color.red));
if (givenAnswer.equals(question.getAnswer2()) && (givenAnswer.equals(question.getCorrect())))
viewHolder.checkBoxScoreTwo.setTextColor(context.getResources().getColor(R.color.holo_blue_dark));
if (givenAnswer.equals(question.getAnswer3()) && !(givenAnswer.equals(question.getCorrect())))
viewHolder.checkBoxScoreThree.setTextColor(context.getResources().getColor(R.color.red));
if (givenAnswer.equals(question.getAnswer3()) && (givenAnswer.equals(question.getCorrect())))
viewHolder.checkBoxScoreThree.setTextColor(context.getResources().getColor(R.color.holo_blue_dark));
if (givenAnswer.equals(question.getAnswer4()) && !(givenAnswer.equals(question.getCorrect())))
viewHolder.checkBoxScoreFour.setTextColor(context.getResources().getColor(R.color.red));
if (givenAnswer.equals(question.getAnswer4()) && (givenAnswer.equals(question.getCorrect())))
viewHolder.checkBoxScoreFour.setTextColor(context.getResources().getColor(R.color.holo_blue_dark));
// check all of the correct answers
if (question.getAnswer1().equals(question.getCorrect()))
viewHolder.checkBoxScoreOne.setChecked(true);
if (question.getAnswer2().equals(question.getCorrect()))
viewHolder.checkBoxScoreTwo.setChecked(true);
if (question.getAnswer3().equals(question.getCorrect()))
viewHolder.checkBoxScoreThree.setChecked(true);
if (question.getAnswer4().equals(question.getCorrect()))
viewHolder.checkBoxScoreFour.setChecked(true);
return convertView;
【问题讨论】:
ListView 正确显示所有信息...(下面的两个建议)...验证被搞砸了...就像 *.equals() 刚刚停止如果我使用“convertView == null”方法工作。 (基本上我只是更改文本颜色并勾选正确答案)......使用 convertView == null...... 几个项目打勾......并且几个项目变成红色而不是一个。 如果我在 ... 中移动验证,它可以工作,但是列表会多次显示一个项目... 好的,我解决了这个问题...我不得不将所有复选标记文本颜色恢复为白色并每次取消它们...这看起来正常吗? 【参考方案1】: convertView = theInflater.inflate(R.layout.score_layout, parent, false);
像这样初始化你的 convertView。应该可以完美运行。
【讨论】:
【参考方案2】:-
将
final
删除到您不需要的ViewHolder
。
在convertView == null
添加:
convertView = theInflater.inflate(R.layout.score_layout, null);
应该会好很多。
【讨论】:
不走运...同样的事情...列表视图显示...只是我的验证由于某种原因停止工作...:-(以上是关于Android ListView 和带有 ViewHolder 的自定义适配器的主要内容,如果未能解决你的问题,请参考以下文章
Android精通:View与ViewGroup,LinearLayout线性布局,RelativeLayout相对布局,ListView列表组件
android listview getviewtypecount和getItemViewType
从 ListView 获取孩子会导致 android.view.View android.view.View.findViewById(int) 在空对象引用上
Android 在listview里itme中有imagebutton 怎么监听这个button?