如何在ListView中的复选框中获取位置

时间:2015-10-16 14:52:10

标签: android listview checkbox android-listview

我有一个带有复选框的listView。我想要做的是当我点击Toast中的复选框显示此复选框的位置时。我正在尝试一些事情,但我现在不知道我做错了什么。我在这里找到了类似的解决方案,但对我不起作用。

在我的代码中,如果您按行,我可以显示Toast,但如果您按下复选框则不会。

提前致谢

public class MyClass extends HmBaseActivity{

    protected ListView listView ;
    protected CheckBox selectDay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.play_all_day);

        listView = (ListView) findViewById(R.id.weekList);
        listView.setChoiceMode(listView.CHOICE_MODE_MULTIPLE);
        selectDay = (CheckBox)findViewById(R.id.checkBox_play_all_day);

       final String[] values = new String[] {
                "Monday",
                "Tuesday",
                "Wednesday",
                "Thursday",
                "Friday",
                "Saturday",
                "Sunday"};

        ArrayAdapter <String> adapter = new ArrayAdapter<String>(this, R.layout.list_days, R.id.play_all_day, values);
        listView.setAdapter(adapter);
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // ListView Clicked item value
                String  itemValue = (String) listView.getItemAtPosition(position);

                Toast.makeText(getApplicationContext(),
                        "Position :" + position + "  ListItem : " + itemValue, Toast.LENGTH_LONG)
                        .show();

            }

        });
    }
}

1 个答案:

答案 0 :(得分:1)

试试这个:

selectDay.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

   @Override
   public void onCheckedChanged(CompoundButton compoundButtonView, boolean isChecked) {

     // ListView Clicked item value
        String  itemValue = (String) listView.getItemAtPosition(position);

        Toast.makeText(getApplicationContext(),
                    "Position :" + position + "  ListItem : " + itemValue, Toast.LENGTH_LONG)
                    .show();
   });