我有自定义列表适配器和复选框我想删除选中项目上的列表项。 在将此片段删除的项目返回列表视图后删除列表视图的项目时。 当检查项目时出现下一个问题(这个问题用于点击正文列表视图而不是点击复选框)删除所有选中的项目不是一起删除大多数点击不止一次删除按钮。 任何人帮助我,我很享受 我的片段
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setOnItemClickListener( new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
chk = (CheckBox) view.findViewById(R.id.checkBox6);
chk.toggle();
}
});
Button btn = (Button) view.findViewById(R.id.button1);
btn.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < list.size()/*this is the original model*/; i++) {
if(list.get(i).isSelected()==true){
adapter.remove(list.get(i));
}
}
adapter.notifyDataSetChanged();
}
})
listadapter
public class ListAdapter extends BaseAdapter {
private final String TAG = "*** ListAdapter ***";
Context context;
LayoutInflater myInflater;
List<contac> list;
CheckBox checkBox6;
public ListAdapter(Context context) {
super();
myInflater = LayoutInflater.from(context);
this.context = context;
Log.i(TAG, "Adapter setuped successfully.");
}
public void remove(contac object) {
list.remove(object);
notifyDataSetChanged();
}
public void setData(List<contac> list) {
this.list = list;
Log.i(TAG, "Data passed to the adapter.");
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = myInflater.inflate(R.layout.list_adapter, null);
holder = new ViewHolder();
// holder.tvId = (TextView) convertView.findViewById(R.id.tvId);
holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
holder.tvPhone = (TextView) convertView.findViewById(R.id.tvPhone);
holder.tvnum = (TextView) convertView.findViewById(R.id.tvnum);
holder.tvtype = (TextView) convertView.findViewById(R.id.tvtype);
holder.tvcode = (TextView) convertView.findViewById(R.id.tvcode);
holder.tvesme = (TextView) convertView.findViewById(R.id.tvesme);
holder.checkBox6 = (CheckBox)convertView.findViewById(R.id.checkBox6);
holder.checkBox6
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
contac element = (contac) holder.checkBox6.getTag();
element.setSelected(buttonView.isChecked());
}
});
convertView.setTag(holder);
holder.checkBox6.setTag(list.get(position));
} else {
holder = (ViewHolder) convertView.getTag();
((ViewHolder) convertView.getTag()).checkBox6.setTag(list.get(position));
}
// ViewHolder holder = (ViewHolder) convertView.getTag();
NumberFormat format = NumberFormat.getCurrencyInstance();
// holder.tvId.setTag(list.get(position).getIdInString());
// holder.tvId.setText(list.get(position).getIdInString());
holder.tvName.setText(format.format(list.get(position).getName()));
holder.tvPhone.setText(format.format(list.get(position).getPhoneNumber()));
holder.tvnum.setText(list.get(position).getNUM());
holder.tvtype.setText(list.get(position).getTYPE());
holder.tvcode.setText(list.get(position).getCODE());
holder.tvesme.setText(list.get(position).getESME());
holder.checkBox6.setChecked(list.get(position).isSelected());
return convertView;
}
static class ViewHolder {
// TextView tvId;
TextView tvName;
TextView tvPhone;
TextView tvnum;
TextView tvtype;
TextView tvcode;
TextView tvesme;
CheckBox checkBox6;
}
public List<contac> getcontac() {
return list;
}
public void remove(Object position) {
list.remove(position);
}
}
联络
public class contac {
private long id;
private int name;
private int phoneNumber;
private String num;
private String code;
private String esme;
private String type;
private boolean selected;
public contac (long id, int name, int phoneNumber,
String num, String code ,String esme, String type) {
this.id = id;
this.name = name;
this.phoneNumber = phoneNumber;
this.code = code;
this.esme = esme;
this.type = type;
selected = false;
}
public long getId() {
return id;
}
public String getIdInString() {
return Long.toString(id);
}
public int getName() {
return name;
}
public String getNameInString() {
return Long.toString(name);
}
public int getPhoneNumber() {
return phoneNumber;
}
public String getPhoneNumberInString() {
return Long.toString(phoneNumber);
}
public String getNUM() {
return num;
}
public String getTYPE() {
return type;
}
public String getCODE() {
return code;
}
public String getESME() {
return esme;
}
public void setId(long id) {
this.id = id;
}
public void setName(int name) {
this.name = name;
}
public void setPhoneNumber(int phoneNumber) {
this.phoneNumber = phoneNumber;
}
public void setNUM(String num) {
this.num = num;
}
public void setTYPE(String type) {
this.type = type;
}
public void setCODE(String code) {
this.code = code;
}
public void setESME(String esme) {
this.esme = esme;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
答案 0 :(得分:0)
如果列表视图内容已更改,请按this答案获取有关如何更新适配器的说明。根据该答案更新您的适配器
private class CustomListAdapter extends ArrayAdapter<CustomModel> {
// you adapter code here...
public void updateList(List<CustomModel> list) {
this.list = list;
notifyDataSetChanged();
}
并将其作为yourAdapterInstance.updateList(newListContent)