使用viewHolder界面更新状态列表视图 [英] using viewHolder Interface to update state to Listview

查看:144
本文介绍了使用viewHolder界面更新状态列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的code在具有getTag()一个ArrayAdapter

内的其他部分

  =持有人(ViewHolder)convertView.getTag();

我怎么用这部分来更新或获得一个视图项目的最新状态,并保持这种状态的保存状态是一致的。就像如果你点击复选框或从数据库中获取的复选框的选中状态,并且想要在复选框的正确状态显示?

在我的应用我收到从数据库中复选框的previous状态和设置复选框,该国在创建列表视图时,如果箱子的状态改变,那么在状态变化将是保存到数据库和复选框也将显示状态变化。

换句话说,我应该放什么在code的那款,除了getTag行?

  IF((convertView == NULL)){                LayoutInflater吹气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.smalltank_customer_row,NULL);                持有人=新ViewHolder();                holder.textViewOne =(TextView中)convertView.findViewById(R.id.textView1);
                holder.textViewTwo =(TextView中)convertView.findViewById(R.id.textView2);
                holder.textViewThree =(TextView中)convertView.findViewById(R.id.textView3);
                holder.radioGroupOne =(RadioGroup中)convertView.findViewById(R.id.radioGroup1);
                holder.radioButtonOne =(单选)convertView.findViewById(R.id.radioButton1);
                holder.radioButtonTwo =(单选)convertView.findViewById(R.id.radioButton2);
                holder.checkBoxOne =(复选框)convertView.findViewById(R.id.checkBox1);
                holder.buttonOne =(按钮)convertView.findViewById(R.id.button1);
                holder.buttonTwo =(按钮)convertView.findViewById(R.id.button2);
                holder.buttonThree =(按钮)convertView.findViewById(R.id.button3);
                holder.buttonFour =(按钮)convertView.findViewById(R.id.button4);                convertView.setTag(保持器);            }其他{                支架=(ViewHolder)convertView.getTag();            }


解决方案

由于convertView可以回收,一般都是很不稳定,通过扩展其viewHolder同样挥发,所以你不应该存储诸如复选框的选择有什么持久性数据。你应该在一些地方保存你的复选框的选择值时,你的活动被暂停和恢复将持续。

作为一个例子,我用了一个HashSet的一个啊,我的应用程序来保存用户选择的触点,这些保存和恢复活动时暂停,恢复,抽样code:

 私人的HashSet<&联络资料GT; selSet =新的HashSet<&联络资料GT;();公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    如果((convertView == NULL)){
        LayoutInflater吹气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.smalltank_customer_row,NULL);        持有人=新ViewHolder();
        //初始化架
        convertView.setTag(保持器);
    }其他{
        支架=(ViewHolder)convertView.getTag();
    }
    holder.status.setChecked(selSet.contains(数据));
    返回convertView;
}公共无效的onClick(视图v){
    UserHolder支架=(UserHolder)v.getTag();
    如果(selSet.contains(holder.user)){
        selSet.remove(holder.user);
    }其他{
        selSet.add(holder.user);
    }
}公众最终无效的onCreate(捆绑savedInstanceState){
    如果(savedInstanceState!= NULL){
        selSet =(HashSet的<&联络资料GT;)savedInstanceState
                .getSerializable(selSet);
    }
}@覆盖
公共无效的onSaveInstanceState(捆绑outState){
    super.onSaveInstanceState(outState);    outState.putSerializable(selSet,selSet);
}

in the code below in the else section that has getTag() inside of the ArrayAdapter

  holder = (ViewHolder) convertView.getTag();

how do I use this section to update or get the latest state of a view item and keep that state consistent with what the saved state is. like if you click a checkbox or get the checked state of the checkbox from the database and want that to be displayed in the correct state of the checkbox?

in my app i am getting the previous state of the checkbox from the database and setting the checkbox to that state when the Listview is created, and if the state of the box is changed then that change in state will be saved to the database and the checkbox will also show the change in state.

in other words what am i supposed to put in that section of code, besides the getTag line?

  if ((convertView == null)){

                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.smalltank_customer_row, null);

                holder = new ViewHolder();

                holder.textViewOne = (TextView) convertView.findViewById(R.id.textView1);
                holder.textViewTwo = (TextView) convertView.findViewById(R.id.textView2);
                holder.textViewThree = (TextView) convertView.findViewById(R.id.textView3);
                holder.radioGroupOne = (RadioGroup) convertView.findViewById(R.id.radioGroup1);
                holder.radioButtonOne = (RadioButton) convertView.findViewById(R.id.radioButton1);
                holder.radioButtonTwo = (RadioButton) convertView.findViewById(R.id.radioButton2);
                holder.checkBoxOne = (CheckBox) convertView.findViewById(R.id.checkBox1);
                holder.buttonOne = (Button) convertView.findViewById(R.id.button1);
                holder.buttonTwo = (Button) convertView.findViewById(R.id.button2);
                holder.buttonThree = (Button) convertView.findViewById(R.id.button3);
                holder.buttonFour = (Button) convertView.findViewById(R.id.button4);

                convertView.setTag(holder);

            }else{

                holder = (ViewHolder) convertView.getTag();

            }

解决方案

Since convertView can be recycled and is generally very volatile, by extension its viewHolder is equally volatile, so you shouldn't store any persistent data such as checkbox selections there. You should store your checkbox selection values somewhere that will be persisted when your Activity is paused and resumed.

As an example, I used a HashSet in one o my app to hold the contacts the user has selected which is saved and restored when the Activity is paused and resumed, sample code:

private HashSet<ContactData> selSet = new HashSet<ContactData>();

public View getView(int position, View convertView, ViewGroup parent) {
    if ((convertView == null)){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.smalltank_customer_row, null);

        holder = new ViewHolder();
        //init holder
        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }
    holder.status.setChecked(selSet.contains(data));
    return convertView;
}

public void onClick(View v) {
    UserHolder holder = (UserHolder) v.getTag();
    if (selSet.contains(holder.user)) {
        selSet.remove(holder.user);
    } else {
        selSet.add(holder.user);
    }
}

public final void onCreate(Bundle savedInstanceState) {
    if (savedInstanceState != null) {
        selSet = (HashSet<ContactData>) savedInstanceState
                .getSerializable("selSet");
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putSerializable("selSet", selSet);
}

这篇关于使用viewHolder界面更新状态列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆