所以目前我有一个列表视图,每行末尾都有复选框。现在,当你勾选方框时,方框会被勾选并且未被勾选。但我想要的是当你点击listview项目/行时,复选框被勾选或未勾选。
这是我的代码:
public class MyActivity3 extends Activity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my3);
Button m = (Button) findViewById(R.id.button3);
tv = (TextView) findViewById(R.id.textViewcat);
Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
tv.setTypeface(typeface);
String listArray[] = new String[] { "All", "Friends & Family", "Sports", "Outside",
"At School", "Fitness", "Photography", "Food", "Beach", "Money" };
ListView listView = (ListView) findViewById(R.id.listView);
List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
for (int i = 0; i <= listArray.length - 1; i++) {
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("title", listArray[i]);
aList.add(hm);
}
String[] sfrm = { "title"};
int[] sto = { R.id.title};
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList,
R.layout.row_layout, sfrm, sto);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long id) {
switch (position) {
}
}
});
}
答案 0 :(得分:1)
@Override
public void onItemClick(AdapterView<?> arg0, View view,position, long id) {
CheckBox cb = (CheckBox) view.findViewById(R.id.your_checkbox);
cb.setChecked(!cb.isChecked());
}
如果不是,则只检查项目,如果是,则取消选中。
答案 1 :(得分:0)
Zoltish的建议对你来说很好,但是如果你创建一个自定义列表适配器,那么对于每个新要求的列表和无错编码的进一步修改会更好
如果提到的教程太复杂,你可以试试这样的
private class checkBoxListAdapter extends BaseAdapter {
private class ViewHolder {
CheckBox chk;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) atvt
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.msg_receipents, null);
holder = new ViewHolder();
holder.chk = (CheckBox) convertView
.findViewById(R.id.checkBox1);
convertView.setTag(holder);
convertView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
holder.chk.setChecked(!holder.chk.isChecked());
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
}