我有一個動態的ListView
它使用ArrayAdapter
。當從微調器中選擇一個名稱時,該名稱連同顯示他們是男性還是女性的圖標將被添加到ListView
。ListView with ArrayAdapter和ViewHolder將圖標添加到錯誤的項目
大部分情況都是好的(名字被正確添加到列表中,並附帶一個圖標)。但顯示性別的圖標被添加到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);
有一些公平的文件有:https://developer.android.com/training/improving-layouts/smooth-scrolling.html – Vince 2014-10-23 20:22:32