这是我之前解决这个问题写的东西,直接贴上来,你看下吧。
private List<Boolean> originalFoundedDevicesState; //有一个保存状态的list
给listview绑定adapter:
private CheckAdapter foundedDevices;
private ListView foundedList;
foundedDevices=new CheckAdapter(AddingDevicesActthis,originalFoundedDevices,originalFoundedDevicesState);
foundedListsetAdapter(foundedDevices);
foundedListsetItemsCanFocus(false);foundedListsetChoiceMode(ListViewCHOICE_MODE_MULTIPLE);foundedListsetOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
} });
2自己为checkbox复写个adapter:
class CheckAdapter extends BaseAdapter{
Context mContext;
List<Device> mData;
List<Boolean>status;
LayoutInflater mInflater;
CheckBox checkBox;
HashMap<Integer,View> lmap=new HashMap<Integer,View>();
public CheckAdapter(Context context,List<Device> data,List<Boolean> DeviceStatus)
{
mInflater=LayoutInflaterfrom(context);
mData=data;
status=DeviceStatus;
}
public int getCount() {
// TODO Auto-generated method stub
return mDatasize();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return mDataget(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(lmapget(position)==null)
{
convertView=mInflaterinflate(Rlayoutchild,parent, false);
checkBox=(CheckBox)convertViewfindViewById(Riddevices);
convertViewsetTag(checkBox);
}
else
{
checkBox=(CheckBox)convertViewgetTag();
}
checkBoxsetId(position);
checkBoxsetChecked(statusget(position));
checkBoxsetText(mDataget(position)toString());
checkBoxsetOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
statusset(buttonViewgetId(), isChecked);
notifyDataSetChanged();
}
});
return convertView;
}
3最后可以根据其是否被选中状态判断某项是否被选中,并提取id:
for(num1=0;num1<originalFoundedDevicesStatesize();num1++)
{
if(originalFoundedDevicesStateget(num1))
finalPairedDevicesadd(new Device(originalFoundedDevicesget(num1)getName(), originalFoundedDevicesget(num1)getAddress()));
}
listView控件有一个属性叫:selectedItems,这是一个集合类型,你可以用索引的方式来取你选择的值,如果你是单选,则是:listView1SelectedItems[0],如果你是多选,则,你可以用foreach或for遍历,如:for(int i=0;i<listView1selectedItemscount;i++){messageBoxshow(listView1SelectedItems[i]ToString());}
希望对你有帮助
ListView listView1 = new ListView();
listView1Bounds = new Rectangle(new Point(10,10), new Size(300,200));
// Set the view to show details
listView1View = ViewDetails;
// Allow the user to edit item text
listView1LabelEdit = true;
// Allow the user to rearrange columns
listView1AllowColumnReorder = true;
// Display check boxes
listView1CheckBoxes = true;
// Select the item and subitems when selection is made
listView1FullRowSelect = true;
// Display grid lines
listView1GridLines = true;
// Sort the items in the list in ascending order
listView1Sorting = SortOrderAscending;
// Create three items and three sets of subitems for each item
ListViewItem item1 = new ListViewItem("item1",0);
// Place a check mark next to the item
item1Checked = true;
item1SubItemsAdd("1");
item1SubItemsAdd("2");
item1SubItemsAdd("3");
ListViewItem item2 = new ListViewItem("item2",1);
item2SubItemsAdd("4");
item2SubItemsAdd("5");
item2SubItemsAdd("6");
ListViewItem item3 = new ListViewItem("item3",0);
// Place a check mark next to the item
item3Checked = true;
item3SubItemsAdd("7");
item3SubItemsAdd("8");
item3SubItemsAdd("9");
// Create columns for the items and subitems
listView1ColumnsAdd("Item Column", -2, HorizontalAlignmentLeft);
listView1ColumnsAdd("Column 2", -2, HorizontalAlignmentLeft);
listView1ColumnsAdd("Column 3", -2, HorizontalAlignmentLeft);
listView1ColumnsAdd("Column 4", -2, HorizontalAlignmentCenter);
以上就是关于如何遍历并获取listview中checkbox是否选中以及该项的id全部的内容,包括:如何遍历并获取listview中checkbox是否选中以及该项的id、ListView中鼠标选中项的下标怎么获取、C# listview 控件中,以列表呈现,如何获取选中的某一项的名称等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)