您好我正在使用android.I创建了一个自定义列表视图,每行都有切换按钮。但问题是当我滚动列表视图时切换按钮状态发生变化。我怎么解决这个问题。请帮我 。在此先感谢:)这是我的代码
class dataListAdapter extends BaseAdapter {
ArrayList<String> name,status;
dataListAdapter() {
name = null;
status = null;
}
public dataListAdapter(ArrayList<String> text,ArrayList<String> text1){
name = text;
status=text1;
notifyDataSetChanged();
}
public int getCount() {
// TODO Auto-generated method stub
return name.size();
}
public void updateResults(ArrayList<String> text) {
name = text;
notifyDataSetChanged();
}
public Object getItem(int arg0) {
return null;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.cmplnt_single_item, parent, false);
TextView name1;
name1 = (TextView) row.findViewById(R.id.name);
name1.setText(name.get(position));
ToggleButton toggle = (ToggleButton) row.findViewById(R.id.toggleButton1);
if(status.get(position).equals("0"))
{
toggle.setChecked(false);
}
else
{
toggle.setChecked(true);
}
toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
{
Toast.makeText(getApplicationContext(), "on at "+position, 5000).show();
}
else
{
Toast.makeText(getApplicationContext(), "off at "+position, 5000).show();
}
}
});
return (row);
}
}
答案 0 :(得分:0)
使用SharedPreference获取切换状态并在切换切换时保存切换状态