我正在使用一个简单的适配器来显示列表,但是在滚动listview时,switch会改变它的状态:
lv = (ListView) findViewById(R.id.listView);
String[] from = {"CompanyName", "AppointmenTime", "Status"};
// view id's to which data to be binded
int[] to = {R.id.clientname, R.id.time, R.id.togglebtn};
//Creating Adapter
android.widget.SimpleAdapter k = new android.widget.SimpleAdapter(ClientListActivity.this, clientData, R.layout.client_list, from, to) {
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
Switch switchcompact=(Switch) v.findViewById(R.id.togglebtn);
switchcompact.setTag(position);
switchcompact.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
{
////some code
}}
答案 0 :(得分:0)
如果您正在滚动,那么如果列表项不可见,则会创建并销毁它们。所以你必须保存Switch的状态:
private Map<Integer, Boolean> states = new Hashmap<>();
public View getView(final int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
Switch switchcompact = (Switch) v.findViewById(R.id.togglebtn);
switchcompact.setChecked(Boolean.TRUE.equals(states.get(position));
switchcompact.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
states.put(position, isChecked);
}
});
}
但是,这只是伪代码。我强烈建议您使用SharedPreferences
保存并获取选择状态。
答案 1 :(得分:0)
在切换状态改变后,您必须更新ArrayList(或提供给listview的数据集)中特定Item的值。
import android.content.Context;
import android.view.LayoutInflater;
导入android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Switch;
import java.util.List;
import java.util.Map;
public class SimpleListViewAdaptor扩展了BaseAdapter {
Context context;
List<Map<String, Object>> data;
public SimpleListViewAdaptor(Context context, List<Map<String, Object>> data) {
super();
this.context=context;
this.data=data;
}
@Override
public Map<String,Object> getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getCount() {
return data.size();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
HolderView holderView;
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.customr_item_list,parent, false);
holderView = new HolderView();
holderView.aSwitch = (Switch) convertView.findViewById(R.id.togglebtn);
convertView.setTag(holderView);
} else {
// View recycled !
// no need to inflate
// no need to findViews by id
holderView = (HolderView) convertView.getTag();
}
holderView.aSwitch.setChecked((Boolean)data.get(position).get("Status"));
holderView.aSwitch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
if(((Switch)v).isChecked()){
((Switch)v).setChecked(true);
data.get(position).put("Status",true);//this is imp to update the value in dataset which is provided to listview
}else{
((Switch)v).setChecked(false);
data.get(position).put("Status",false);
}
}
});
return convertView;
}
static class HolderView{
Switch aSwitch;
}
}