我有一个使用 ArrayAdapter
的动态 ListView
。当从微调器中选择一个名字时,该名字连同一个显示他们是男性还是女性的图标被添加到 ListView
。
大多数情况下一切都很好(名称与图标一起正确添加到列表中)。但是显示性别的图标被添加到 ListView
中的错误项目。该名称被添加到列表的底部,但图标被放置在列表顶部的名称处。我不知道这是否是我使用 ViewHolder
的方式,但是 Android website 中关于它的文档为零.
// Listview inflater
inflater = (LayoutInflater) (this).getSystemService(LAYOUT_INFLATER_SERVICE);
// List Array.
mAdapter = new ArrayAdapter<String>(this, R.layout.player_simple_list,
R.id.label, mStrings) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.i("ANDY","View getView Called");
// A ViewHolder keeps references to children views to
// avoid unneccessary calls to findViewById() on each row.
ViewHolder holder;
if (null == convertView) {
Log.i("ANDY","Position not previously used, so inflating");
convertView = inflater.inflate(R.layout.player_simple_list, null);
// Creates a ViewHolder and store references to the
// two children views we want to bind data to.
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.label);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
if (sexmale == true) {
holder.icon.setImageBitmap(maleicon);
}
else {
holder.icon.setImageBitmap(femaleicon);
}
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
// Bind the data efficiently with the holder.
holder.text.setText(getItem(position));
// Change icon depending is the sexmale variable is true or false.
Log.i("ANDY","getCount = "+mAdapter.getCount());
return convertView;
}
};
setListAdapter(mAdapter);
最佳答案
您必须在 if-else-if
之后设置图标以创建或绑定(bind) holder
。否则,图标将仅在列表中的前几项中正确显示,即直到未填充 ListView
。
public View getView(int position, View convertView, ViewGroup parent) {
Log.i("ANDY","View getView Called");
// A ViewHolder keeps references to children views
// to avoid unneccessary calls to findViewById() on each row.
ViewHolder holder;
if (null == convertView) {
Log.i("ANDY","Position not previously used, so inflating");
convertView = inflater.inflate(R.layout.player_simple_list, null);
// Creates a ViewHolder and store references to
// the two children views we want to bind data to.
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.label);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
// Bind the data efficiently with the holder.
holder.text.setText(getItem(position));
// Change icon depending is the sexmale variable is true or false.
if (sexmale == true) {
holder.icon.setImageBitmap(maleicon);
}
else {
holder.icon.setImageBitmap(femaleicon);
}
Log.i("ANDY","getCount = "+mAdapter.getCount());
return convertView;
}
关于android - 带有 ArrayAdapter 和 ViewHolder 的 ListView 将图标添加到错误的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3123193/