使用getTag和setTag来保存ListItem值

时间:2016-02-04 11:07:59

标签: android listview baseadapter

我正在学习getTag和setTag。我需要解释这些方法,并想知道它们是如何工作的。 我创建了一个简单的CustomListView,它有两个按钮和一个textview。 按钮是加减法,它增加或减少计数器的值,但问题是只有最后一个项值正在改变。请给我一个解决方案以及一个很好的解释 谢谢。

@Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_items, null);
            add = (Button) convertView.findViewById(R.id.add);
            sub = (Button) convertView.findViewById(R.id.sub);
            textView = (TextView) convertView.findViewById(R.id.numberTV);
            convertView.setTag(new ViewHolder(add, sub, textView , count));
        }
        holder = (ViewHolder) convertView.getTag();
        textView = holder.textView;
        count = holder.counter;
        add = holder.add;
        sub = holder.sub;
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count +=1;
                textView.setText(String.valueOf(count));
            }
        });
        sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count -=1;
                textView.setText(String.valueOf(count));
            }
        });
        return convertView;
    }
}

class ViewHolder {

    Button add, sub;
    TextView textView;
int counter = 0;
    public ViewHolder(Button add, Button sub, TextView textView , int counter) {
        this.add = add;
        this.sub = sub;
        this.counter = counter;
        this.textView = textView;
    }
} 

1 个答案:

答案 0 :(得分:0)

custom listview adapter中,您要实施的内容是recycling中显示的single item (list_items) Listview。因此,您需要对该视图使用getTagsetTag方法。

那你为什么要首先要这些方法......

  1. 您正在尝试回收list_items并在其中查看,为此您正在制作ViewHolder,其中包含list_items内部视图的内部参考
  2. 您不想调用inflater并且每次调用getView时都会对视图进行引用,因此您将ViewHolder's实例保存到视图中,该视图将再次使用没有重新膨胀list_items及其中的引用。
  3. setTag(),您可以将对象附加到View
  4. 通过getTag(),您可以取回附加到View
  5. 的对象

    因此,每次调用list_items时,您都会循环使用getView()的相同初始化

    似乎做了很多工作,请使用RecyclerView

    您的代码解决方案......您可以查看gitHub

    上的代码
    public class ListViewTestFiveActivity extends AppCompatActivity{
    
        private ListView listView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_list_view_test_five);
    
    
            initializeUI();
        }
    
        private void initializeUI() {
            listView = (ListView)findViewById(R.id.ListViewTestFiveActivity_listView);
            ArrayList<Item> items = new ArrayList<>();
    
            for (int i = 0; i < 15; i++) {
                Item item = new Item();
                item.setCount(i);
                items.add(item);
            }
    
            MyAdapter adapter = new MyAdapter(getApplicationContext(), R.layout.single_item_listview_five, items);
            listView.setAdapter(adapter);
        }
    
        private class MyAdapter extends ArrayAdapter{
    
            private ArrayList<Item> items;
            private Context a_context;
            private LayoutInflater a_layoutInflater;
    
            public MyAdapter(Context context, int resource, ArrayList<Item> items) {
                super(context, resource, items);
                this.a_context = context;
                this.items = items;
                a_layoutInflater = LayoutInflater.from(this.a_context);
            }
    
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View row = convertView;
                ViewHolder holder = null;
                if (row == null) {
                    row = a_layoutInflater.inflate(R.layout.single_item_listview_five, parent, false);
                    holder = new ViewHolder();
                    holder.plus = (Button) row.findViewById(R.id.ListViewTestFiveActivity_plus_button);
                    holder.textView = (TextView) row.findViewById(R.id.ListViewTestFiveActivity_count_textView4);
                    holder.minus = (Button) row.findViewById(R.id.ListViewTestFiveActivity_minus_button);
                    row.setTag(holder);
                } else {
                    holder = (ViewHolder) row.getTag();
                }
    
                final Item item = items.get(position);
                holder.textView.setText("" + item.getCount());
                holder.plus.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int increase_count =item.getCount();
                        item.setCount(++increase_count);
                        notifyDataSetChanged();
                    }
                });
                holder.minus.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int decrease_count =item.getCount();
                        item.setCount(--decrease_count );
                        notifyDataSetChanged();
                    }
                });
                return row;
            }
    
            private class ViewHolder{
                Button plus;
                TextView textView;
                TextView minus;
            }
        }
    
        private class Item {
            int count;
    
            public int getCount() {
                return count;
            }
    
            public void setCount(int count) {
                this.count = count;
            }
        }
    }
    
      

    输出enter image description here