Android ListView与checkbox和所有可点击的

可能重复:
Android:从数据库绑定数据到ListView中的checkbox?

我想要使​​用具有以下布局的项目的ListView

------------------------- [CB] TV TV ------------------------- 

CB是checkbox,电视是Textview。

现在我读了一个地方,你不能有一个ListView中的可点击的项目。 如果你有一些,那么你将无法点击ListItems。 但是,如果我看看GoogleMail应用程序,这是可能的。 我可以用checkbox标记几个消息(然后为他们select一个动作),或者我可以点击ListItem(或滚动与dpad)到另一个屏幕。 有人有代码/例如这是可能的吗?

在您的XML布局中将CheckBox设置为focusable="false" 。 否则,它将从列表视图中窃取点击事件。

当然,如果你这样做,你需要手动处理标记CheckBox选中/取消选中,如果列表项被点击,而不是CheckBox ,但你可能想要的。

将列表视图适配器设置为“simple_list_item_multiple_choice”

 ArrayAdapter<String> adapter; List<String> values; // put values in this //Put in listview adapter = new ArrayAdapter<UserProfile>( this, android.R.layout.simple_list_item_multiple_choice, values); setListAdapter(adapter); 
 holder.checkbox.setTag(row_id); 

 holder.checkbox.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { CheckBox c = (CheckBox) v; int row_id = (Integer) v.getTag(); checkboxes.put(row_id, c.isChecked()); } }); 

这个代码适用于我的项目,我可以select列表视图项目和checkbox

 <?xml version="1.0" encoding="utf-8"?> <!-- Single List Item Design --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:clickable="true" > <TextView android:id="@+id/label" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="4" /> <CheckBox android:id="@+id/check" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:focusable="false" android:text="" > </CheckBox> </LinearLayout> 

以下代码将帮助您:

 public class DeckListAdapter extends BaseAdapter{ private LayoutInflater mInflater; ArrayList<String> teams=new ArrayList<String>(); ArrayList<Integer> teamcolor=new ArrayList<Integer>(); public DeckListAdapter(Context context) { // Cache the LayoutInflate to avoid asking for a new one each time. mInflater = LayoutInflater.from(context); teams.add("Upload"); teams.add("Download"); teams.add("Device Browser"); teams.add("FTP Browser"); teams.add("Options"); teamcolor.add(Color.WHITE); teamcolor.add(Color.LTGRAY); teamcolor.add(Color.WHITE); teamcolor.add(Color.LTGRAY); teamcolor.add(Color.WHITE); } public int getCount() { return teams.size(); } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { final ViewHolder holder; if (convertView == null) { convertView = mInflater.inflate(R.layout.decklist, null); holder = new ViewHolder(); holder.icon = (ImageView) convertView.findViewById(R.id.deckarrow); holder.text = (TextView) convertView.findViewById(R.id.textname); .......here you can use holder.text.setonclicklistner(new View.onclick. for each textview System.out.println(holder.text.getText().toString()); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.text.setText(teams.get(position)); if(position<teamcolor.size()) holder.text.setBackgroundColor(teamcolor.get(position)); holder.icon.setImageResource(R.drawable.arraocha); return convertView; } class ViewHolder { ImageView icon; TextView text; } } 

希望这可以帮助。