0

I have a code where the user is shown a list of items. A toggle button is placed alongside the list items. So when the user sets toggle on then an alarm is triggered. Now the problem is when I set the toggle button of 1st item in the list it vanishes and / or resets when I scroll to the bottom of the list view and come back again to the first item. I am totally confused on setting this to stay there selected. Request someone in this forum to help me out.

My class CSVAdapter. I am attaching here.

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();
        }

}
}
Krish3090
  • 79
  • 2
  • 14
  • possible duplicate of [ListView Viewholder checkbox state](http://stackoverflow.com/questions/16350670/listview-viewholder-checkbox-state) – josephus May 30 '14 at 01:27

1 Answers1

1

There's different ways you can accomplish this. One way is to create a member variable ArrayList. Something like

private HashMap<Integer, Boolean> btnState = new HashMap<Integer, Boolean>();

then when the button state is true just us that position in getView() and add it there as true. Then in your getView() check each time if the item at that position is true or false and set the state with that value.

public View getView(int pos,View convertView,final ViewGroup parent){

  ...
  myviewholder.tb.setChecked(btnState.get(position);  

And in your onCheckChangedListener obviously you will want to add the value to your list

btnState.put(position, isChecked); // is checked is whatever value you use in your listener

You could also use SparseBooleanArray or ArrayList but I believe HashMap worked best for my needs for whatever reason.

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • Thanks CodeMagic ! Will try that out and get back to you – Krish3090 May 30 '14 at 02:22
  • Bro i'm getting a force close. Null Pointer Exception in the stack trace. Btw where should I put that code of yours myviewholder.tb.setChecked(btnState.get(position)); Inside the getView method or inside the if(convertView==null) ??? Confused with this one. Need your help. – Krish3090 May 30 '14 at 16:30
  • What line is giving you the `NPE`, bro? Do you have an `onCheckChangedListener()`? If so, then there. If not then inside your `getView()`. After your `if/else` – codeMagic May 30 '14 at 16:32
  • if i put that code inside the onchecedchangelistener() , it gives me a force close during the button check event. Else if i place the code after if/else statements, then it gives me a force close while drawing the list itself. Either way, this didn't work out :( .. If u want I can post the complete code.. Do you ? – Krish3090 May 30 '14 at 17:12
  • You have to initialize your button like you do your TextView. I assumed you knew that – codeMagic May 30 '14 at 22:57
  • magic : Im sorry.. Im a newbie in android development. how do i do that and where should i initialize it? if you can provide the code , that will be like so good for me to understand. – Krish3090 May 31 '14 at 00:48
  • Where do you initialize `myviewholder.text`? Do the same with your button – codeMagic May 31 '14 at 00:52
  • yeah i did that..If u can see my ViewHolder() class.. I did initialize both my toggle button and the textview.. – Krish3090 May 31 '14 at 00:53
  • No, you need to initialize them in `getView()` – codeMagic May 31 '14 at 00:55
  • i did initialize them in getView()..getting a force close now :( – Krish3090 May 31 '14 at 00:56
  • for your clear understanding.. I will post the complete code... I will update them in the question itself. Please go through 'em and lemme know where have I done mistake(s)? – Krish3090 May 31 '14 at 01:00
  • And what is the logcat exception you are getting now? – codeMagic May 31 '14 at 01:07
  • now i made the code somehow work.. but the point is.. Im checking the first item in the list, scrolling down and getting back to the first item is unchecked and randomly many other items are checked... – Krish3090 May 31 '14 at 01:12