gpt4 book ai didi

java - 我正在为 ListView 使用简单适配器,但在 ListView 中滚动时开关会更改其状态?

转载 作者:太空宇宙 更新时间:2023-11-04 12:09:09 24 4
gpt4 key购买 nike

我使用一个简单的适配器来显示列表,但开关在滚动 ListView 时更改其状态:

  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
}}

最佳答案

开关状态更改后,您必须更新 ArrayList(或提供给 ListView 的数据集)中特定项目的值。

导入 android.content.Context;

导入 android.view.LayoutInflater;

导入android.view.View;

导入android.view.ViewGroup;

导入android.widget.BaseAdapter;

导入 android.widget.Switch;

导入java.util.List;

导入java.util.Map;

公共(public)类 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;
}

}

关于java - 我正在为 ListView 使用简单适配器,但在 ListView 中滚动时开关会更改其状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40037065/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com