我按照本教程http://xjaphx.wordpress.com/2011/06/16/viewholder-pattern-caching-view-efficiently/。但是,它並不奏效。你們能幫我怎麼做這個工作嗎?ViewHolder無法正常工作
class Custom extends Activity {
private ListView listview;
private ArrayList mListItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom);
listview = (ListView) findViewById(R.id.list_view);
mListItem = ItemsDisplay.getItems(true);
listview.setAdapter(new CustomListAdapter(this, R.id.list_view,mListItem));
}
static class ViewHolder {
public TextView textView;
}
private class CustomListAdapter extends ArrayAdapter {
private ArrayList mList; //--CloneChangeRequired
private Context mContext;
public CustomListAdapter(Context context, int textViewResourceId,
ArrayList list) { //--CloneChangeRequired
super(context, textViewResourceId, list);
this.mList = list;
this.mContext = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
final ToggleButton tb;
System.out.println("position "+position+" mList.size"+mList.size());
if (view == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.list_item_bn, null);
ViewHolder holder = new ViewHolder();
holder.textView = (TextView)convertView.findViewById(R.id.tv_name);
view.setTag(holder);
}
ItemsDisplay listItem = (ItemsDisplay) mList.get(position);
System.out.println("listItem.getName() " +listItem.getName());
if (listItem != null) {
ViewHolder holder1 = (ViewHolder)view.getTag();
holder1.textView.setText(listItem.getName());
}
return view;
}
}
}
我已經看過這些帖子,但我不知道爲什麼我的代碼仍然沒有工作。 「如果」 條件和getTag 你必須setTag()中: 解決方案 - : How to implement a view holder?
這給了我一個想法,雖然。謝謝... – antonoVodka 2012-03-12 08:29:59
你也可以看看這個代碼爲什麼它不工作?我想要做的就是在列表視圖的最後一項上使用不同的佈局。 – antonoVodka 2012-03-12 08:49:05
int lastpos = mList.size() - 1; System.out.println(「position:」+ position +「mlist:」+ lastpos); if(position == lastpos){ view = vi.inflate(R.layout.list_item_record,null); holder.textView =(TextView)view.findViewById(R.id.record_view);其他{ view = vi.inflate(R.layout.list_item_bn,null); holder.textView =(TextView)view.findViewById(R.id.tv_name); } view.setTag(holder); – antonoVodka 2012-03-12 08:49:15