gpt4 book ai didi

java - 多选 ListView 包含复选框

转载 作者:太空宇宙 更新时间:2023-11-04 15:04:46 24 4
gpt4 key购买 nike

我有带有复选框的 ListView,应用程序删除选中的项目;;并且应该删除与选中的项目具有相同 id 的项目:这是我的代码

private class CAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<Entity> list;
private Context context;
String Status;
CAdapter(Context context,
ArrayList<Entity> getC) {
this.context = context;
this.list = getC;
Status="";
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

class ViewHolder {
TextView Name;
TextView Desc;

Button deleteBtn;

CheckBox CBox;
}

public int getCount() {
return list.size();
}

public Object getItem(int position) {
return list.get(position);
}

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




@SuppressLint("NewApi")
public View getView(final int position, View convertView, ViewGroup parent) {

final ViewHolder holder;
final CEntity CObj = list.get(position);

if (convertView == null) {
convertView = mInflater.inflate(
R.layout.custom_list_view_confirmed, parent,
false);
holder = new ViewHolder();


holder.Name = (TextView) convertView
.findViewById(R.id.Name);



holder.Desc = (TextView) convertView
.findViewById(R.id.activity1);





holder.deleteBtn = (Button) convertView
.findViewById(R.id.deleteBtn);

holder.CBox=(CheckBox) convertView.findViewById(R.id.isCheck);

convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}








if (CObj.getMystatus().equals(
context.getResources().getString(R.string.course_status_delete))) {
holder.status.setTextColor(Color.RED);
} else if (attemptedCourseObj.getMystatus().equals(
context.getResources().getString(R.string.course_status_pending))) {
holder.status.setTextColor(Color.GREEN);
} else if (attemptedCourseObj.getMystatus().equals(
context.getResources().getString(R.string.course_status_update))) {
holder.status.setTextColor(Color.BLUE);
}

holder.Name.setText(attemptedCourseObj.getCourseName());







holder.CBox.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(holder.CBox.isChecked()){
if(list.contains(getItem(position))){
list.remove(getItem(position));
}
}
}

});
//

return convertView;
}
}

问题是当删除选中的项目时,不会删除具有相同 id 的项目。

int pos=Data.CList.size();
SparseBooleanArray checked=CListView.getCheckedItemPositions();
for (int n = pos; n > 0; n--){
if (checked.get(n)){
code=Data.inList.get(n).getCCode();
Data.inList.remove(n);

}else if(CList.get(n).equal(code){
Data.inList.remove(n);
}

最佳答案

尝试使用notifydatasetchanged刷新列表,并确保从列表中删除整个对象

关于java - 多选 ListView 包含复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22148242/

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