你好我想在itemonsetchenge listner中取消选中复选框时删除元素。 这是我的代码
checkbox_timeslot.clear();
chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
String chetimeslot;
if(isChecked){
chetimeslot = (String)chk.getTag();
checkbox_timeslot.add(chetimeslot);
Log.e("array value", ""+checkbox_timeslot);
Log.e("checked slo value", ""+chetimeslot);
}else{
checkbox_timeslot.add("");
Log.e("else array value", ""+checkbox_timeslot);
}
}
});
" checkbox_timeslot"是我的字符串数组list.i想要删除复选框中的元素是未选中的,并从复选框列表中选中时添加项目。如何可能有帮助?
答案 0 :(得分:6)
if(isChecked){
chetimeslot = (String)chk.getTag();
checkbox_timeslot.add(chetimeslot);
Log.e("array value", ""+checkbox_timeslot);
Log.e("checked slo value", ""+chetimeslot);
} else{
chetimeslot = (String)chk.getTag();
checkbox_timeslot.remove(chetimeslot);
Log.e("else array value", ""+checkbox_timeslot);
}
答案 1 :(得分:0)
使用add(item)将元素添加到数组列表中。删除(项目)以从列表中删除。
chetimeslot = (String)chk.getTag();
if(isChecked){
checkbox_timeslot.add(chetimeslot);
} else{
checkbox_timeslot.remove(chetimeslot);
}
答案 2 :(得分:0)
if(isChecked){
chetimeslot = (String)chk.getTag();
checkbox_timeslot.add(chetimeslot);
}else{
chetimeslot = (String)chk.getTag();
checkbox_timeslot.remove(chetimeslot);
}
}
答案 3 :(得分:0)
今天我有同样的问题。这就是我解决它的方式。 我使用RecyclerView发布了我的整个适配器类。
package com.aqua.machinetest;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Danish.sharma on 5/9/2017.
*/
public class GovernateListAdapter extends RecyclerView.Adapter<GovernateListAdapter.MyViewHolder> {
private List<Governate> governateList;
public static ArrayList<String> arrayListSelectedContacts = new ArrayList<>();
Context context;
public GovernateListAdapter(List<Governate> governateList, Context context) {
this.governateList = governateList;
this.context = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.governate_list_row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
Governate governate = governateList.get(position);
holder.title.setText(governate.getName());
holder.checkBox.setChecked(governateList.get(position).getCheckedBox());
holder.checkBox.setTag(governateList.get(position));
holder.checkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Governate governate1 = (Governate) cb.getTag();
governate1.setCheckedBox(cb.isChecked());
governateList.get(position).setCheckedBox(cb.isChecked());
System.out.println("selected contacts list " + governate1.getId());
//Toast.makeText(v.getContext(), contact.getName() + " is " + cb.isChecked(), Toast.LENGTH_LONG).show();
if (cb.isChecked()) {
arrayListSelectedContacts.add(governateList.get(position).getId());
} else {
arrayListSelectedContacts.remove(governateList.get(position).getId());
}
System.out.println("selected contacts list 3 " + arrayListSelectedContacts);
Toast.makeText(context, arrayListSelectedContacts.size() + " Added", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return governateList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public CheckBox checkBox;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.textView_Title);
checkBox = (CheckBox) view.findViewById(R.id.checkBox);
}
}
}
这是我的pojo课程:
package com.aqua.machinetest;
import android.os.Parcel;
import android.os.Parcelable;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Danish.sharma on 5/9/2017.
*/
public class Governate {
String id;
String name;
Boolean checkedBox = false;
// List<Governate> governateList = new ArrayList<>();
public Governate() {
}
public Governate(String id, String name) {
this.id = id;
this.name = name;
// this.governateList = governateList;
}
protected Governate(Parcel in) {
id = in.readString();
name = in.readString();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boolean getCheckedBox() {
return checkedBox;
}
public void setCheckedBox(Boolean checkedBox) {
this.checkedBox = checkedBox;
}
}
At Last:这是我的片段,我实现了onClickListener
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.linear_Next:
try {
if (GovernateListAdapter.arrayListSelectedContacts.size() == 0) {
Toast.makeText(getActivity(), "No data selected.", Toast.LENGTH_SHORT).show();
} else if (GovernateListAdapter.arrayListSelectedContacts.size() > 0) {
Toast.makeText(getActivity(), "Selected data=" + GovernateListAdapter.arrayListSelectedContacts.size(),
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}