我有一项任务。我在主题列表中列出了一些列表视图。在特定的列表项中,我输入了textview和复选框。我的问题是,每当我点击listitem(列表中的单个项目)时,它应该分别选中复选框。我正在使用listadpter来显示主题列表,请帮助我。
heading_txt = (TextView) findViewById(R.id.heading_txt);
selectall_txt = (TextView) findViewById(R.id.selectall_txt);
ListViewAdapter adapter = new ListViewAdapter(this,
R.layout.institutionitem, clgList, false, position);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
System.out.println("position value is-->" + position);
ListViewAdapter adapter = new ListViewAdapter(
InstitutionActivity.this, R.layout.institutionitem,
clgList, Check, position);
listView.setAdapter(adapter);
}
});
并列出适配器
public ListViewAdapter(Context context, int layoutResourceId,
ArrayList<String> data, boolean check, int position) {
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
this.Check = check;
this.pos = position;
System.out.println("printdata" + data);
System.out.println("position value " + position);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public static class listHolder {
TextView college_name;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
listHolder holder = null;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layoutResourceId, parent, false);
holder = new listHolder();
holder.college_name = (TextView) row
.findViewById(R.id.college_name);
college_checkb = (CheckBox) row.findViewById(R.id.college_chk);
college_checkb
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked == true) {
InstitutionActivity.itemPos.add(data
.get(position));
// System.out.println("POSITION checked");
} else {
// System.out.println("POSITION uncheked");
InstitutionActivity.itemPos.remove(data
.get(position));
}
}
});
row.setTag(holder);
} else {
holder = (listHolder) row.getTag();
}
holder.college_name.setText(data.get(position));
return row;
}
答案 0 :(得分:1)
我找到了解决方案
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
System.out.println("checcked item" + position);
Log.e("Check ITEM", "" + position);
CheckBox c = (CheckBox) view.findViewById(R.id.sub_chk);
if (c.isChecked()) {
c.setChecked(false);
} else {
c.setChecked(true);
}
}
});