我有一个代码,向用户显示项目列表。切换按钮放置在列表项旁边。因此,当用户设置切换时,就会触发警报。现在的问题是,当我在列表中设置第一项的切换按钮时,当我滚动到列表视图的底部并再次返回到第一项时,它消失和/或重置。我对将其设置为保持选中状态感到非常困惑。请求此论坛中的某人帮助我。
我的班级 CSVAdapter。我附在这里。
public class CSVAdapter extends ArrayAdapter<clock>{
Context ctx;
String line;
int position=-1;
ViewHolder myviewholder;
mrvtoparanur mp;
clock clk;
HashMap<Integer, Boolean> btnstate=new HashMap<Integer,Boolean>();
public CSVAdapter(Context context,int textViewResourceId, ArrayAdapter<clock> clk)
{
super (context,R.layout.checkbox,textViewResourceId);
this.ctx = context;
loadArrayFromFile();
}
static class ViewHolder
{
TextView text;
ToggleButton tb;
}
@Override
public View getView(final int pos,View convertView,final ViewGroup parent){
View row=convertView;
if(row==null){
LayoutInflater vi=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row=vi.inflate(R.layout.checkbox, parent,false);
myviewholder.text=(TextView)convertView.findViewById(R.id.code);
myviewholder.tb=(ToggleButton)convertView.findViewById(R.id.checkBox1);
myviewholder=new ViewHolder();
position=pos;
row.setTag(myviewholder);
//Log.d("KEY", "VIEW CREATED NEWLY");
//myviewholder.tb.setChecked(btnstate.get(pos));
}
else
{
myviewholder=(ViewHolder)convertView.getTag();
Log.d("KEY", "VIEW RECYCLED");
btnstate.size();
btnstate.put(position, true);
}
myviewholder.text.setText(getItem(pos).getTime());
myviewholder.tb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
//myviewholder.tb.setChecked(btnstate.get(position));
btnstate.putAll(btnstate);
btnstate.put(position, true);
}
});
return row;
}
private void loadArrayFromFile(){
try
{
InputStream is=ctx.getAssets().open("mrvtoparanur.csv");
BufferedReader reader=new BufferedReader(new InputStreamReader(is));
//Read each line
while((line = reader.readLine())!=null){
clock cur = new clock();
cur.setTime(line);
this.add(cur);
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}