列表与导航抽屉中的复选框 [英] list with checkBoxes in a navigation drawer
问题描述
我试图做一个带有复选框列表视图的导航抽屉。作为每个列表项。这是我到目前为止所尝试的,但是我有很大的困难。
Im trying to make a navigation drawer with a listview that has check boxes in it. as each list item. this is what i have tried so far , But im having great difficulty.
什么im寻找是创建一个prefence菜单。
iebluetooth on / off(checkbox)etc
what im looking to is create a prefence menu. i.e "bluetooth on/off (checkbox)" etc
Heres我的主要活动:
Heres My Main Activity:
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);
}
出现错误说适配器需要文本视图。
任何帮助将非常感谢。感谢
Im getting an error saying that the adapter needs a text view. Any help would be greatly appreciated. Thanks
推荐答案
您得到的错误是,您必须传递一个TextView而不是R.layout.list_item,示例:
The error you are getting is beacuse you have to pass a TextView instead of R.layout.list_item, check the below example:
mAdapter = new ArrayAdapter<String>(this, R.layout.my_textview, osArray);
要创建 ListView 复选框,请将ChoiceMode设置为CHOICE_MODE_MULTIPLE / p>
To create a ListView with checkboxes set the ChoiceMode to "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);
结果:
这篇关于列表与导航抽屉中的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!