gpt4 book ai didi

android - List item点击check勾选放在android中的listview

转载 作者:行者123 更新时间:2023-11-29 01:41:04 24 4
gpt4 key购买 nike

我有一个任务。我在主题列表中列出了一些 ListView 。在特定的列表项中,我放置了 TextView 和复选框。我的问题是,每当我点击列表项(列表的单个项目)时,它应该分别选中复选框。我正在使用 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;

}

最佳答案

我找到了解决方案

        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);

}

}
});

关于android - List item点击check勾选放在android中的listview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24648140/

24 4 0