本文介绍了ViewHolder不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面这个教程的.然而,它不是工作。你们可以帮助我如何做这方面的工作?

I was following this tutorial http://xjaphx.wordpress.com/2011/06/16/viewholder-pattern-caching-view-efficiently/. however, it isnt working. Can you guys help me how to make this working?

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;  
        }

    }
}

我已经看着这些帖子,但林不知道为什么我的code仍然心不是工作。

http://stackoverflow.com/questions/3123193/listview-with-arrayadapter-and-viewholder-adding-icons-to-the-wrong-item

i already looked into these posts, but im not sure why my code still isnt working.How to implement a view holder?http://stackoverflow.com/questions/3123193/listview-with-arrayadapter-and-viewholder-adding-icons-to-the-wrong-item

推荐答案

更改为:

        final ToggleButton tb;

         System.out.println("position "+position+" mList.size"+mList.size());
         if (convertView == null) {

          LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          convertView = vi.inflate(R.layout.list_item_bn, null);
          ViewHolder holder = new ViewHolder();
          holder.textView = (TextView)convertView.findViewById(R.id.tv_name);
          convertView.setTag(holder);
         }

         ItemsDisplay listItem = (ItemsDisplay) mList.get(position);
         System.out.println("listItem.getName() " +listItem.getName());
            if (listItem != null) {

             ViewHolder holder1 = (ViewHolder)convertView.getTag();
             holder1.textView.setText(listItem.getName());

            }

        return convertView;  

这篇关于ViewHolder不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 12:18