解决android中listview中嵌套checkbox滑动时checkbox状态改变问题

如题

listview中嵌套checkbox,当滑动时会自动调用checkbox的setOnClickListener方法

导致checkbox的状态在滑动时发生改变

解决办法就是在getview里初始化每个checkbox之前,设置setOnClickListener为null

以下为本人代码:

其中红色为重要位置

package com.sourceplan.activity;



import java.util.List;
import java.util.Map;


import com.example.source.R;


import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;


public class Plan_show_adapter extends BaseAdapter {


private Context context;
private List> listitems;
private LayoutInflater layoutInflater;
private Boolean[] ifchecked;
private Plan_show plan_show;
static int checkboxtag=1;

public Boolean[] getIfchecked() {
return ifchecked;
}
public void setIfchecked(Boolean[] ifchecked) {
this.ifchecked = ifchecked;
}
private final class ListItem {
public CheckBox checkBox;
public TextView time;
public TextView thing;
public Button button;


}


public Plan_show_adapter(Context context, List> listitems,Plan_show activity) {
// TODO Auto-generated constructor stub
this.context = context;
layoutInflater = LayoutInflater.from(context);
this.listitems = listitems;
plan_show=activity;
init();
}
private void init()
{
ifchecked = new Boolean[getCount()];
for(int i=0;i ifchecked[i]=false;
}
}
public void changeallcheckboxstate(){
int i;
if(checkboxtag==1){
for(i=0;i ifchecked[i]=true;

}
checkboxtag=0;

}
else{
for(i=0;i ifchecked[i]=false;

}
checkboxtag=1;
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return listitems.size();
}


@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return listitems.get(position);
}


@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

private void showinfo(int id){
new AlertDialog.Builder(context)
.setTitle("结果")
.setMessage("共找到"+10000*id+"相似的人")
.setPositiveButton("去看看", null).show();
}
private void checkedChange(int checkedID) {   
ifchecked[checkedID] = !ifchecked[checkedID];   
    }
public boolean hasChecked(int checkedID) {   
        return ifchecked[checkedID];   
    }
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final int thisid=position;
ListItem listItem = null;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.plan_show_unit, null);
listItem=new ListItem();
listItem.button = (Button) convertView.findViewById(R.id.bt_plan_show_search);
listItem.checkBox = (CheckBox) convertView.findViewById(R.id.cb_plan_show_unit_checkbox);
listItem.thing = (TextView) convertView.findViewById(R.id.tv_plan_show_unit_thing);
listItem.time = (TextView) convertView.findViewById(R.id.tv_plan_show_unit_clock);
convertView.setTag(listItem);
} else {
listItem = (ListItem) convertView.getTag();
}
//解决checkbox在listview中滑动状态改变问题
listItem.checkBox.setOnCheckedChangeListener(null);
//初始化为null

listItem.checkBox.setChecked(ifchecked[position]);
listItem.button.setText((String) listitems.get(position).get("button"));
listItem.thing.setText((String) listitems.get(position).get("thing"));
listItem.time.setText((String) listitems.get(position).get("time"));
listItem.button.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.v("MyListViewBase", "你点击了按钮" + thisid);
//showinfo(thisid);
// Intent intent=new Intent(context,Plan_new.class);
// startActivity(intent);
Intent intent=new Intent(context,Plan_search.class);
intent.putExtra("lastactivity", "show");
plan_show.startActivity(intent);

plan_show.finish();
}
});
listItem.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
checkedChange(thisid);
}
});
return convertView;
}


}


相关文章:http://xiaomi4980.blog.163.com/blog/static/215945196201421992856631/

你可能感兴趣的:(解决android中listview中嵌套checkbox滑动时checkbox状态改变问题)