【问题标题】:Checking the checked item in list view在列表视图中检查选中的项目
【发布时间】:2011-12-10 04:33:28
【问题描述】:

嗨,我正在尝试实现一个长列表,用户可以在其中检查列表视图中的项目。当他滚动列表项时,我将检查的项目存储在 sparseboolean 数组中,但是当我尝试单击按钮检查项目时检查哪些项目。它显示出奇怪的行为。当我检查第一个项目并单击按钮时,它给出了正确的结果,但是当我开始检查列表底部的项目时。然后它显示奇怪的行为。有 14 个项目从 0-13 索引。当我检查第 13 个元素时,它显示没有 toast。当我单击第 12 个元素时,它显示没有 toast。当我单击第 12 个元素时,它显示没有 toast 第一个 toast 出现时我点击第 8 个元素。请帮我找出我错的地方并帮助我修复错误。当我点击第 7 个元素时,三个 toast 出现 7,8,9 当我点击第 6 个元素 5toast 出现 6,7, 8,9,10.请帮我修复这个错误。我想在 toast 中显示被选中的数字。 这是我的代码

/** 
 * This example shows how to create a list of checkboxes. 
 */ 
public class CheckboxActivity extends ListActivity implements 
AdapterView.OnItemClickListener { 
    static SparseBooleanArray mCheckStates; 
    private CheckBoxAdapter mCheckBoxAdapter;
    List<Boolean> check;
    Button btn;
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mCheckBoxAdapter = new CheckBoxAdapter(this, 
R.layout.list_item_checkbox, GENRES); 
        setListAdapter(mCheckBoxAdapter); 
        check=new ArrayList<Boolean>();
        final ListView listView = getListView(); 
        listView.setItemsCanFocus(false); 
        listView.setTextFilterEnabled(true); 
        listView.setOnItemClickListener(this);
        btn=(Button)findViewById(R.id.btn);
        btn.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                for(int i=0;i<mCheckStates.size()+1;i++)
                    if(mCheckStates.get(i)==true)
                        Toast.makeText(CheckboxActivity.this,i+" " ,Toast.LENGTH_SHORT).show();
            }

        });
    } 
    public void onItemClick(AdapterView parent, View view, int 
position, long id) { 
        mCheckBoxAdapter.toggle(position); 
    } 
    private static class CheckBoxAdapter extends ArrayAdapter<String> 
            implements CompoundButton.OnCheckedChangeListener { 

        public CheckBoxAdapter(Context context, int resource, String[] 
objects) { 
            super(context, resource, objects); 
            mCheckStates = new SparseBooleanArray(objects.length); 
        } 
        @Override 
        public View getView(int position, View convertView, ViewGroup parent) { 
            final CheckBox view = (CheckBox) super.getView(position, 
convertView, parent); 
            view.setTag(position); 
            view.setChecked(mCheckStates.get(position, false)); 
            view.setOnCheckedChangeListener(this); 
            return view; 
        } 
        public boolean isChecked(int position) { 
            return mCheckStates.get(position, false); 
        } 
        public void setChecked(int position, boolean isChecked) { 
            mCheckStates.put(position, isChecked); 
            notifyDataSetChanged(); 
        } 
        public void toggle(int position) { 
            setChecked(position, !isChecked(position)); 
        } 
        public void onCheckedChanged(CompoundButton buttonView, 
boolean isChecked) { 
            mCheckStates.put((Integer) buttonView.getTag(), isChecked); 
        } 
    } 
    private static final String[] GENRES = new String[] { 
        "Action", "Adventure", "Animation", "Children", "Comedy", 
"Documentary", "Drama", 
        "Foreign", "History", "Independent", "Romance", "Sci-Fi", 
"Television", "Thriller" 
    }; 
} 

【问题讨论】:

  • 嗯?!请考虑将您的段落分成几块。很难理解描述您检查项目流程的 5 行句子。
  • @Jack 嗨,我想现在我正确解释了我的问题,请帮忙。
  • 你有想过这个吗?我刚刚注意到,在 btn onClickListener 的 for 循环中,您将从 i=1 变为 mCheckStates.size() + 1,我认为应该只是 mCheckStates.size()

标签: android android-layout android-widget


【解决方案1】:

这个方法在做什么?

public void onCheckedChanged(CompoundButton buttonView, 
                            boolean isChecked) { 
    mCheckStates.put((Integer) buttonView.getTag(), isChecked); 
}

您的列表行中是否有按钮?

编辑:实际上 - 请参阅 this 问题,因为我认为这会有所帮助。

【讨论】:

  • view.setOnCheckedChangeListener(this) 在此它实现了 CheckBox 的 OnCheckedChangListener 的功能。没有列表行没有按钮按钮在列表视图之外单击时显示已检查的项目。
【解决方案2】:

刚刚尝试回答类似的问题Here

其次,这种奇怪的行为是因为 ListView 重用了您必须维护复选框状态的视图 sp,并在您的适配器的 getView 中使用该状态。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多