如何获得多选框选中的值?使用了listview,每个列表项表框一个显示的textview和一个checkbox,请问如何取得值呢?我使用了网上这位仁兄的办法,没有起作用,代码如下:
// 去掉相同的商品 List<Instrument> instList = APIDoc.getSystemDocCaptain() .getInstrumentList(); listems = new ArrayList<Map<String, Object>>(); for (Instrument instrument : instList) { Map<String, Object> listem = new HashMap<String, Object>(); listem.put("instrument", instrument.getInstrument()); listems.add(listem); } SimpleAdapter simplead = new SimpleAdapter(getApplicationContext(), listems, R.layout.activity_systeminstrument_item, new String[] { "instrument" }, new int[] { R.id.addsysteminstrument }); instrumentList = (ListView) findViewById(R.id.systeminstrumentlistview); instrumentList.setAdapter(simplead); // final ListView listView = getListView(); // listView.setItemsCanFocus(false); instrumentList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); instrumentList.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { System.out.println("你点击了" + "************"); System.out.println("你点击了" + "************"); System.out.println("你点击了" + "************"); System.out.println("你点击了" + "************"); System.out.println("你点击了" + "************"); System.out.println("你点击了" + "************"); System.out.println("你点击了" + "************"); List list = new ArrayList(); SparseBooleanArray a = instrumentList.getCheckedItemPositions(); for (int i = 0; i < listems.size(); i++) { if (a.valueAt(i)) { Long val = instrumentList.getAdapter().getItemId( a.keyAt(i)); System.out.println(val + "************"); System.out.println(val + "************"); System.out.println(val + "************"); System.out.println(val + "************"); System.out.println(val + "************"); System.out.println(val + "************"); System.out.println(val + "************"); System.out.println(val + "************"); list.add(instrumentList.getAdapter().getItemId( (a.keyAt(i)))); } } for (Iterator i = list.iterator(); i.hasNext();) { System.out.println(i.next()); } } });
解决方案:20分
checkbox不是有事件监听吗
解决方案:30分
ListView.setOnItemClickListener(),重载这个监听器中的onItemClick()方法,里面会获得一个position变量,应该就是你想要的值