在android中迭代listView,使每个项目前面的复选框可见



我使用android记事本教程代码开始。在我的notes_row.xml中,我有一个显示注释标题的textview,以及一个最初由android隐藏的复选框:visibility="gone"

我希望当用户打开选项菜单并单击标记时,列表中每个项目前面的复选框应该变得可见。但是,当我单击模拟器中的标记时,只有第一个音符的复选框变得可见。我想遍历整个列表,setVisibility(0)让所有复选框都可见。我是初学者,请详细回答。提前谢谢。

我的代码片段:
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    switch(item.getItemId()) {
    case INSERT_ID:
        createNote();
        return true;
    case MARK_ID:
                // only first item check box appears with following 2 lines:
                    CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox1);
                    checkBox.setVisibility(0);
                /* tried the following, but no chekbox appears with this:   
        Cursor c = mDbHelper.fetchAllNotes();
        c.moveToPosition(0);
        while(c.isAfterLast()){
            CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox1);
            checkBox.setVisibility(0);
            c.moveToNext();
        }
                 */
        return true;
}

我的新代码在阅读答案后,仍然只有第一个项目的复选框出现:

public boolean onMenuItemSelected(int featureId, MenuItem item) {
    switch(item.getItemId()) {
    case INSERT_ID:
        createNote();
        return true;
    case MARK_ID:
        CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox1);
        String[] lvItems = new String[]{NotesDbAdapter.KEY_TITLE};
        MyAdapter arrAdapter = new MyAdapter(this, android.R.layout.simple_list_item_multiple_choice, lvItems);
        arrAdapter.toggleCheckBoxVisibility(checkBox);
        arrAdapter.notifyDataSetChanged();
                    return true;
    return super.onMenuItemSelected(featureId, item);
             }

这是我的适配器

private class MyAdapter extends ArrayAdapter<String>{
    public MyAdapter(Context context, int textViewResourceId,
            String[] objects) {
        super(context, textViewResourceId, objects);
        // TODO Auto-generated constructor stub
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.notes_row, parent, false);
        //String[] from = new String[]{NotesDbAdapter.KEY_TITLE};
        return row;
        //return super.getView(position, convertView, parent);
    }
    public boolean toggleCheckBoxVisibility(CheckBox checkBox){
        checkBox.setVisibility(0);
        super.notifyDataSetChanged();
        return true;
    }
}

在适配器中创建一个方法来为可见性设置布尔标志。在按钮上单击调用该方法并将标志设置为true。然后使用notifyDataSetChanged()来更新您的列表。在getView方法中,您可以将复选框的可见性设置为布尔标志中的值。

  1. 带上你的列表适配器
  2. 使用getView(或类似)遍历列表项的视图
  3. 对于你获取的每个视图做v.setVisibility(view . visible)

这大概就是你与列表的每个视图交互所要做的事情

编辑:更具体地说:在activity主线程中

MyAdapter a= (MyAdapter) this.getArrayAdapter(); 
//count how many views you have to iterate and build a for loop accordingly
for(...){
    View v = a.getView(/*based on the for index position */).setVisibility(View.VISIBLE) 
//Do something else with the view if you want...

这个解决方案假设你已经创建了一个数组适配器并与你的activity关联。

试试这个:在您的类中编写以下代码:

@Override
protected void onStart() {
    super.onStart();
    ArrayAdapter<String> araAdapter=new ArrayAdapter<String>(getApplicationContext(),             
    android.R.layout.simple_list_item_multiple_choice, xmlList);
    listView_xml.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    listView_xml.setAdapter(araAdapter);
}

xmlList是要保存的数据或字符串[]数组。Listview_xml是listview的名称。通过本教程实现多个复选框

最新更新