【问题标题】:Why my own ListAdapter does not work?为什么我自己的 ListAdapter 不起作用?
【发布时间】:2011-11-02 15:49:19
【问题描述】:

我遇到的问题是,当我单击复选框并使列表视图向上滚动选中的复选框时,或者有时如果选择第一个复选框,那么最后一个复选框也被选中。我知道我的英语还不够好,很抱歉。任何帮助表示赞赏。

import java.util.ArrayList;
import java.util.Hashtable;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;

public class MyListAdapter extends BaseAdapter{

     public static class ViewHolder {

         public CheckBox chkb = null;

    } 


    private LayoutInflater mInflater;

    private ArrayList<MyListAdapterItem> myListAdapterItems = null;

    private Hashtable<Object, Object> items = null;

    private boolean selectAll = false;

    private boolean readOnly = false;

    public MyListAdapter(Context context, ArrayList<MyListAdapterItem> myListAdapterItems, boolean selectAll, boolean readOnly) {

        mInflater = LayoutInflater.from(context);

        this.myListAdapterItems = myListAdapterItems;

        this.selectAll = selectAll;

        this.readOnly = readOnly;

        items = new Hashtable<Object, Object>();
    }

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

        ViewHolder viewHolder = null;

        if (convertView == null) {

            convertView = mInflater.inflate(R.layout.chkb_fila, null);

            viewHolder = new ViewHolder();

            viewHolder.chkb = (CheckBox) convertView.findViewById(R.id.chkbCultivo);

            convertView.setTag(viewHolder);

        }else {

            viewHolder = (ViewHolder) convertView.getTag();

        }

        items.put(myListAdapterItems.get(position), viewHolder.chkb);

        viewHolder.chkb.setText(myListAdapterItems.get(position).getDescription());

        if(selectAll){

            viewHolder.chkb.setChecked(true);
        }

        if(readOnly){

            viewHolder.chkb.setEnabled(false);

        }

        return convertView;

    }


    public int getCount() {

        return myListAdapterItems.size();

    }


    public Object getItem(int position) {

        return myListAdapterItems.get(position);

    }


    public long getItemId(int position) {

        return position;

    }

    /**
     * @return the myListItems
     */
    public ArrayList<MyListAdapterItem> getMySelectedListItems() {

        ArrayList<MyListAdapterItem> listSelectedItems = new ArrayList<MyListAdapterItem>();

        CheckBox tmpCheckBox = null;

        for(int i=0; i<items.size(); i++){

            tmpCheckBox = (CheckBox) items.get(myListAdapterItems.get(i));

            if(tmpCheckBox.isChecked()){

                listSelectedItems.add(myListAdapterItems.get(i));

            }

        }

        return listSelectedItems;

    }

}

【问题讨论】:

  • 谢谢 我改了getView的方法,像这样:
  • 所以我将复选框的状态保存在布尔数组中:private boolean[] states = null;。使用私有 Hashtable itemsPosiciones = null;保存引用复选框,因此当其中一个更改其状态时,我只需保存状态: states[itemsPosiciones.get((CheckBox)arg0)] = arg1;
  • public View getView(int position, View convertView, ViewGroup parent) { convertView = mInflater.inflate(R.layout.chkb_fila, null); CheckBox chkb = (CheckBox) convertView.findViewById(R.id.chkbCultivo); chkb.setText(myListAdapterItems.get(position).getDescription()); chkb.setChecked(状态[位置]); chkb.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton arg0, boolean arg1) { states[itemsPosiciones.get((CheckBox)arg0)] = arg1; } });itemsPosiciones.put(chkb, position);return convertView ;}

标签: android android-listview baseadapter listview-adapter


【解决方案1】:

问题是getView 方法重用了视图,因此您必须确保在该方法中更新复选框的状态;这也意味着您必须将该状态保存在某处。

【讨论】:

  • 谢谢我只是找到一种简单的方法来保存复选框的状态。
【解决方案2】:

这里有一些您正在寻找的链接。高级,但如果你坚持下去并且不放弃这一点,你会学到很多!看看:

Android: ListView elements with multiple clickable buttons

Android custom list item with nested widgets

Rating bars and buttons on listItems

【讨论】:

    【解决方案3】:

    2 个选项。

    1) 不要重复使用您的视图。总是给一个新的充气。这很“懒惰”,但如果您的应用没有太多条目,则不太可能引入错误。

    2) 确保在每个视图上设置所有内容。您正在重复使用它们,所以如果您之前在其中设置了一些东西,它将保持这种状态。

    改变这个:

    if(selectAll){
    
            viewHolder.chkb.setChecked(true);
        }
    
        if(readOnly){
    
            viewHolder.chkb.setEnabled(false);
    
        }
    

            viewHolder.chkb.setChecked(selectAll);
            viewHolder.chkb.setEnabled(!readOnly);
    

    使用 if 块,您无需设置布尔值的另一端(您也可以使用 else 块,但这更冗长)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多