来自setChoiceMode的CheckBox事件监听器(ListView.CHOICE_MODE_MULTIPLE)

时间:2014-01-28 17:22:51

标签: android listview checkbox

所以我有一个listview,我想添加复选框。

    lv = (ListView)findViewById(R.id.list);

    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, listItems);
    lv.setAdapter(adapter);

    lv.setItemsCanFocus(true);
    lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

这样可以显示复选框。然后我的setOnItemClickListener()用于我的列表视图,因为用户需要选择一个项目,然后下一个活动将被启动

lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,int position, long id)
        {
            Intent components = new Intent();

            components.setClass(context, ComponentsActivity.class);
            components.putExtra("studyID", studyID);
            components.putExtra("studyName", studyName);

            startActivity(components);
        }
});

但是,我想添加一个复选框,以便用户可以在列表视图中勾选该项以执行其他操作。问题是我无法区分事件。当我点击复选框时,它会被检查,但随后列表项也会被选中并开始新的活动。我只希望复选框在点击它时受到影响,而不是让它启动新的活动。我知道你也可以创建自己的适配器,但如果我可以在2行代码中创建一个复选框,为什么还要烦。有什么建议?我只是希望能够检查文本框并获取已检查项目的ID。

1 个答案:

答案 0 :(得分:1)

我从来没有找到任何我想要的东西,所以我咬了一口,决定学习如何制作我自己的自定义适配器类。如果有人碰到这个问题,这是我的代码。此适配器类用于带有文本(TextView)和复选框的列表视图。

public class CustomAdapter extends BaseAdapter
{

ArrayList<String> studies;
Context context;
LayoutInflater myInflater;
ArrayList<Boolean> positionArray;

public CustomAdapter(ArrayList<String> arr, Context c)
{
    studies = arr;
    context = c;
    myInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    positionArray = new ArrayList<Boolean>();

    for(int i = 0; i < studies.size(); ++i)
    {
        positionArray.add(false);
    }
}

@Override
public int getCount() {
    return studies.size();
}

@Override
public Object getItem(int i) {
    return studies.get(i);
}

@Override
public long getItemId(int i) {
    return 0;
}

public void remove(int i)
{
    this.studies.remove(i);
    this.positionArray.remove(i);
}

@Override
public View getView(int position, View view, ViewGroup viewGroup)
{
    final int pos = position;
    Holder holder = null;

    //Create the views and populate it with an element from teh array
    if(view == null)
        view = myInflater.inflate(R.layout.custom_list_layout, viewGroup, false);//made my own layout for each listview 'cell'

    holder = new Holder();

    TextView study = (TextView)view.findViewById(R.id.adapterTextView);
    holder.ckbox = (CheckBox)view.findViewById(R.id.adapterCheckBox);

    holder.ckbox.setOnCheckedChangeListener(null);

    study.setText(studies.get(position));

    holder.ckbox.setFocusable(false);
    //Since this method gets called whenever we scroll(view recycling), we have to re-check the checkboxes
    holder.ckbox.setChecked(positionArray.get(position));

    holder.ckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            //checkBoxArray[pos].setChecked(isChecked);
            positionArray.set(pos, isChecked);
        }
    });


    return view;
}

static class Holder
{
    CheckBox ckbox;
}

}

相关问题