此图显示了listview
的外观
当我们选中All时,应该检查所有复选框,当取消选中时,应取消选中所有复选框,这里是ListView
All也是列表视图的一部分,它在地图列表中的位置为0 ...
public class MultiSelectBaseAdapter extends BaseAdapter{
Context context;
LayoutInflater inflater;
List<Boolean> selectedList;
Map<Integer, MultipleSelect> mapList;
public MultiSelectBaseAdapter(Context context, Map<Integer, MultipleSelect> mapList) {
this.context = context;
this.mapList = mapList;
selectedList = new ArrayList<>();
for(int i=0 ; i< mapList.size() ; i++){
selectedList.add(i, mapList.get(i).isSelected());
}
this.inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public List<Boolean> getSelected(){
return selectedList;
}
public Map<Integer, MultipleSelect> getMapList(){
return mapList;
}
@Override
public int getCount() {
if(mapList != null){
return mapList.size();
}
return 0;
}
@Override
public Object getItem(int position) {
return mapList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.multiselect_item_layout, parent, false);
holder = new ViewHolder();
holder.textView = (TextView) convertView.findViewById(R.id.textview_multiselect);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox_multiselect);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.checkBox.setTag(position);
holder.textView.setText(mapList.get(position).getKeyValueType().getName());
if(selectedList.get(position)){
holder.checkBox.setChecked(true);
} else{
holder.checkBox.setChecked(false);
}
holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int index = (Integer)buttonView.getTag();
if(isChecked){
selectedList.set(index, true);
}else{
selectedList.set(index, false);
}
notifyDataSetChanged();
}
});
return convertView;
}
public static class ViewHolder {
TextView textView;
CheckBox checkBox;
}
}
请帮助我我是android的新手,我没有使用CheckBox
[在此输入图像说明] [2]
答案 0 :(得分:0)
在selectedList中添加所有选定的项ID,然后调用适配器 notifyDataSetChanged()方法
答案 1 :(得分:0)
为您的MultipleSelect
模型添加布尔值,并在选中或取消选中时更改它,并在onBindViewHolder
中显示,检查此值并设置复选框状态。
选择全部为地图中的每个项目将此值更改为true
并致电:
yourMultiselectAdapter.notifyDataSetChanged();
同样取消选择所有 - 值为true
及以上的电话)
答案 2 :(得分:0)
@Override
public void onClick(View v) {
String btnlbl = SelectAll.getText().toString();
if(btnlbl.equalsIgnoreCase("SelectAll"))
{
// TODO Auto-generated method stub
CustomListAdapter adapter=new CustomListAdapter(Approver.this,AttendanceModelList,1);
lv1.setAdapter(adapter);
SelectAll.setText("Uncheck All");
}else
{
// TODO Auto-generated method stub
CustomListAdapter adapter=new CustomListAdapter(Approver.this, AttendanceModelList,2);
lv1.setAdapter(adapter);
SelectAll.setText("SelectAll");
}
}
});
这是我的适配器端代码 公共类CustomListAdapter扩展了ArrayAdapter {
private final Activity context;
/* private final List<String> empname;
private final List<String> reason;
private final List<String> empno;
private final List<String> empdate;
private final List<String> empDay ;
private final List<String> empaid;
*/
private Dialog dialog;
private Dialog dialog1;
private Dialog dialogz;
private AlertDialog BackDialog;
int cheki;
Long id;
String Givenregon;
boolean checkAll_flag = false;
boolean checkItem_flag = false;
private final List<AttendanceModel> AttendanceModelList ;
private TextView NameShow;
private TextView NoShow;
private TextView DayShow;
private TextView DateShow;
private TextView ReasonShow;
private Button Approve;
private Button Cansel;
private Button Reject;
//Date date;
public CustomListAdapter(Activity context, List<AttendanceModel> AttendanceModelList,int cheki)
{
super(context, R.layout.mylist,AttendanceModelList);
// TODO Auto-generated constructor stub
this.context=context;
this.AttendanceModelList =AttendanceModelList;
this.cheki =cheki;
}
static class ViewHolder {
protected TextView txtTitle;
protected TextView textemployeedate;
protected ImageButton View1;
protected TextView textempday;
protected CheckBox Chek1 ;
}
public View getView(int position,View convertview,ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertview == null){
LayoutInflater inflater=context.getLayoutInflater();
convertview=inflater.inflate(R.layout.mylist, null);
viewHolder = new ViewHolder();
viewHolder.txtTitle = (TextView) convertview.findViewById(R.id.idemployeename);
//TextView extratxt = (TextView) rowView.findViewById(R.id.idreason);
//TextView txemployee = (TextView) rowView.findViewById(R.id.idempno);
viewHolder. textemployeedate = (TextView)convertview.findViewById(R.id.idempdate);
viewHolder. textempday = (TextView) convertview.findViewById(R.id.idempday);
/* Button Submit = (Button) rowView.findViewById(R.id.btapprove);
*
*/
/*Button Cansel = (Button) rowView.findViewById(R.id.btreject);*/
viewHolder. View1 =(ImageButton)convertview.findViewById(R.id.btmylistviewshow);
viewHolder. Chek1 = (CheckBox)convertview.findViewById(R.id.checkmylist);
viewHolder.Chek1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
AttendanceModelList.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
}
});
convertview.setTag(viewHolder);
convertview.setTag(R.id.idemployeename,viewHolder.txtTitle);
convertview.setTag(R.id.idempday,viewHolder.textempday);
convertview.setTag(R.id.idempdate,viewHolder.textemployeedate);
convertview.setTag(R.id.checkmylist,viewHolder.Chek1);
}
else {
viewHolder = (ViewHolder) convertview.getTag();
}
viewHolder.Chek1.setTag(position);
viewHolder.Chek1.setChecked(AttendanceModelList.get(position).isSelected());
viewHolder.txtTitle.setText(AttendanceModelList.get(position).getEmpname());
if(cheki==1)
{
viewHolder. Chek1.setChecked(true);
}
else if(cheki==2)
{
viewHolder. Chek1.setChecked(false);
}
return convertview;
};
答案 3 :(得分:0)
MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
ListView listView;
String Name[]={"All","One","Two","Three","Four","Five"};
boolean Some[]={false,false,false,false,false,false};
MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
listView = (ListView) findViewById(R.id.listView);
adapter = new MyAdapter(MainActivity.this,Name,Some);
listView.setAdapter(adapter);
}
public void myDAta1(int pos)
{
Some[pos]=false;
adapter.notifyDataSetChanged();
}
public void myDAta2(int pos)
{
Some[pos]=true;
adapter.notifyDataSetChanged();
}
public void myDAta3()
{
for(int i=0;i<Name.length;i++)
{
Some[i]=false;
}
adapter.notifyDataSetChanged();
}
public void myDAta4()
{
for(int i=0;i<Name.length;i++)
{
Some[i]=true;
}
adapter.notifyDataSetChanged();
}
}
MyAdapter
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
String Name[];
boolean Some[];
Context context;
LayoutInflater inflater;
MyAdapter(Context mContext,String mName[],boolean mSome[])
{
this.context=mContext;
this.Name= mName;
this.Some=mSome;
}
@Override
public int getCount() {
return Name.length;
}
@Override
public Object getItem(int position) {
return Name[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) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_layout, parent, false);
holder = new ViewHolder();
holder.textView = (TextView) convertView.findViewById(R.id.textView);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(Name[position]);
holder.checkBox.setChecked(Some[position]);
if(position==0) {
if (holder.checkBox.isChecked()) {
holder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (context instanceof MainActivity) {
((MainActivity) context).myDAta3();
}
}
});
} else {
holder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (context instanceof MainActivity) {
((MainActivity) context).myDAta4();
}
}
});
}
}
else
{
if(holder.checkBox.isChecked())
{
holder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(context instanceof MainActivity){
((MainActivity)context).myDAta1(position);
}
}
});
}
else
{
holder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(context instanceof MainActivity){
((MainActivity)context).myDAta2(position);
}
}
});
}
}
return convertView;
}
public static class ViewHolder {
TextView textView;
CheckBox checkBox;
}
}
答案 4 :(得分:0)
替换此代码
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int index = (Integer)buttonView.getTag();
if(index==0)
{
if (isChecked)
{
for (int i = 0; i < mapList.size(); i++)
{
MultipleSelect obj = (MultipleSelect) getMapList().get(i);
// obj.setSelected(false);
// arrayList2.set(i, obj);
if(obj.isSelected())
{
selectedList.set(i, false);
}
else
{
selectedList.set(i, true);
}
}
}
else
{
selectedList.set(index, false);
}
}
else
{
if (isChecked)
{
selectedList.set(index, true);
}
else
{
selectedList.set(index, false);
selectedList.set(0, false);
}
}
notifyDataSetChanged();
}
});
return convertView;
}
public static class ViewHolder {
TextView textView;
CheckBox checkBox;
}