gpt4 book ai didi

android - 如何使用自定义适配器和 ListView 获取复选框值

转载 作者:行者123 更新时间:2023-11-29 17:34:18 24 4
gpt4 key购买 nike

我正在尝试获取选中的值,但它没有显示该值,我已经在那里实现了自定义适配器,但我不知道如何更新位置

我的代码是..

   public Add_member_adapter(Context a, ArrayList<ItemsModel> d) {

context = a;
data = d;
itemChecked = new boolean[data.size()];

inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

public int getCount() {
if (data.size() <= 0)
return 1;
return data.size();
}

public final Object getItem(int position) {
return data.get(getCount() - position - 1);
}

public final long getItemId(int position) {
return position;
}

public static class ViewHolder {

public TextView Name, UserId_Fk;
CheckBox chkbox;

}

public View getView(final int position, View convertView, ViewGroup parent) {

Log.d("getview:", "position=" + position);
vi = convertView;
final ViewHolder holder;

if (convertView == null) {

vi = inflater.inflate(R.layout.add_members_list_item, null);
holder = new ViewHolder();

holder.Name = (TextView) vi.findViewById(R.id.Name);
holder.UserId_Fk = (TextView) vi.findViewById(R.id.CusId_Fk);
holder.chkbox = (CheckBox) vi.findViewById(R.id.chkbox);

vi.setTag(holder);
} else {

holder = (ViewHolder) vi.getTag();

}
if (data.size() <= 0) {

vi.setVisibility(View.GONE);

} else {

tempValues = null;
tempValues = (ItemsModel) data.get(position);

holder.Name.setText(tempValues.getItemName().toString());
holder.UserId_Fk.setText(tempValues.getUserId_Fk().toString());
holder.chkbox.setChecked(false);



holder.chkbox.setChecked(itemChecked[position]);

holder.chkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
itemChecked[position] = isChecked;
}
});
}
return vi;
}

我正在声明另一个 Activity 中的 List setOnItemClickListener

list.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {

CheckBox checkbox = ((CheckBox) view.findViewById(R.id.chkbox));

String Name = list.getItemAtPosition(position).toString();



}

});

最佳答案

ListView 项内的任何可点击 View 都会窃取 ListView onItemClickListener 点击。

如果你想在你的复选框上捕获检查事件,你应该使用 CompoundButton.OnCheckedChangeListener 而不是 View.OnClickListener。

由于 ListView 上的 convertView 是可重复使用的,您必须意识到您的数据将不准确。您必须创建一个数据集来保存您想要保留的值。

此代码来自您的适配器

public View getView(final int position, View convertView, ViewGroup parent) {
Log.d("getview:", "position=" + position);
vi = convertView;
final ViewHolder holder;

if (convertView == null) {

vi = inflater.inflate(R.layout.add_members_list_item, null);
holder = new ViewHolder();

holder.Name = (TextView) vi.findViewById(R.id.Name);
holder.UserId_Fk = (TextView) vi.findViewById(R.id.CusId_Fk);
holder.chkbox = (CheckBox) vi.findViewById(R.id.chkbox);

vi.setTag(holder);
} else {

holder = (ViewHolder) vi.getTag();

}
if (data.size() <= 0) {

vi.setVisibility(View.GONE);

} else {

tempValues = null;
tempValues = (ItemsModel) data.get(position);

holder.Name.setText(tempValues.getItemName().toString());
holder.UserId_Fk.setText(tempValues.getUserId_Fk().toString());

holder.chkbox.setChecked(checkedHolder[position]);

holder.chkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkedHolder[position] = isChecked;
}
});
}
return vi;
}

public boolean[] checkedHolder;

private void createCheckedHolder() {
checkedHolder = new boolean[getCount()];
}

在适配器的构造函数上调用 createCheckedHolder。而如果想获取checked值,可以从checkedHolder中获取

从 checkedHolder 中,您可以获取用户选择的任何项目的位置,然后您可以从您的 Activity 中调用以下代码

private ArrayList<String> getSelectedNames(){
List<String> names = new ArrayList<String>();
for (int i = 0; i < adapter.getCount(); i++) {
if(adapter.checkedHolder[i]){
//get all name values that checked by user
names.get(i).add(yourDataSet.getItemName().toString());
}
}
return names
}

关于android - 如何使用自定义适配器和 ListView 获取复选框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31339850/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com