列表中包含导航抽屉中的复选框

时间:2016-03-23 20:20:29

标签: android listview checkbox navigation-drawer

我试图制作一个带有列表视图的导航抽屉,里面有复选框。作为每个列表项。这是我到目前为止所尝试的,但我很难。

我想要的是创建一个prefence菜单。 即“蓝牙开/关(复选框)”等

继承人我的主要活动:

public class MainActivity extends ActionBarActivity {

//Navigation Drawer Member Variables
private ListView mDrawerList;
private DrawerLayout mDrawerLayout;
private ArrayAdapter<String> mAdapter;
private ActionBarDrawerToggle mDrawerToggle;
private String mActivityTitle;

//Navigation Drawer

    mDrawerList = (ListView)findViewById(R.id.navList);
    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);

    mActivityTitle = getTitle().toString();

    addDrawerItems();
    setupDrawer();

 private void addDrawerItems() {
    String[] osArray = { "Bluetooth", "Reply to Calls", "Reply to sms", "customise message"};
    mAdapter = new ArrayAdapter<String>(this, R.layout.list_item, osArray);
    mDrawerList.setAdapter(mAdapter);

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

            CheckedTextView ctv = (CheckedTextView)view;
            if (ctv.isChecked()){
                Toast.makeText(getApplicationContext(),"uncheckd",Toast.LENGTH_LONG).show();
            }
            else {
                Toast.makeText(getApplicationContext(),"checked",Toast.LENGTH_LONG).show();
            }

        }
    });
}



    mDrawerToggle.setDrawerIndicatorEnabled(true);
    mDrawerLayout.setDrawerListener(mDrawerToggle);
}

我得到一个错误,说适配器需要文本视图。 任何帮助将不胜感激。感谢

1 个答案:

答案 0 :(得分:2)

您得到的错误是因为您必须传递TextView而不是R.layout.list_item,请查看以下示例:

 mAdapter = new ArrayAdapter<String>(this, R.layout.my_textview, osArray);

使用复选框创建 ListView ,将ChoiceMode设置为“CHOICE_MODE_MULTIPLE”

    String[] osArray = {"Bluetooth", "Reply to Calls", "Reply to sms", "customise message"};
    ListView listView = (ListView) findViewById(R.id.listView);
    ArrayAdapter arrayAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_multiple_choice, osArray);

    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    listView.setAdapter(arrayAdapter);

<强>结果: enter image description here