0

What is the syntax to get a specific togglebutton from a listview containing the said togglebuttons within a listview_row layout?

I would like to initiate the state of each togglebutton (based on some values originating from a Database), within the onCreate method. I have the following code within a loop, but I am not sure how to change it to reference a specific togglebutton from the listview.

   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mydb = new DBHelper(this);
    ArrayList array_list = mydb.getAllAlarms();

    for(int i = 0; i < array_list.size(); i++) {
        arrayListItem = array_list.get(i).toString();
        activationInt = Integer.parseInt(arrayListItem);

        LayoutInflater vi = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = vi.inflate(R.layout.listview_row, null);
        alarm_activated = (ToggleButton) view.findViewById(R.id.alarm_activated);

        if (activationInt == 1) {
            alarm_activated.setChecked(true);
            alarm_activated.getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.MULTIPLY);
        } else {
            alarm_activated.setChecked(false);
        }

    }

    ArrayAdapter arrayAdapter =
            new ArrayAdapter(this, listview_row,R.id.alarm_name,array_list);
    obj = (ListView)findViewById(R.id.listViewAlarms);
    obj.setAdapter(arrayAdapter);
    obj.setOnItemClickListener(new OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                long arg3) {
            String itemVal =(String) arg0.getItemAtPosition(arg2);

            Bundle dataBundle = new Bundle();
            dataBundle.putString("name", itemVal);
            Intent intent = new
                    Intent(getApplicationContext(),DisplayAlarm.class);
            intent.putExtras(dataBundle);
            startActivity(intent);
        }
    });
user229044
  • 232,980
  • 40
  • 330
  • 338
JF0001
  • 819
  • 7
  • 30
  • https://stackoverflow.com/questions/13116425/list-view-implementation-with-text-vew-and-toggle-button-i This can help you – Umair Iqbal Aug 19 '17 at 12:41
  • Thank you Umair, the link provided was indeed helpful. Meanwhile, I have changed the code as now displayed, but it is still not changing the state of the togglebutton. – JF0001 Aug 19 '17 at 13:50
  • Can you add more code, like where this part is executed ? Inflating views like this seems strange to me, if they are item views, should be done in adapter. – smora Aug 19 '17 at 13:57
  • Hi smora. I have added the entire onCreate Method. Thank you for your help. Joseph – JF0001 Aug 19 '17 at 14:07
  • ok, so you'll have to create your custom adapter, because here you are inflating "view" but is not added to any parent. See my answer about dataset, and follow this kind of example https://stackoverflow.com/questions/8166497/custom-adapter-for-list-view – smora Aug 19 '17 at 14:15
  • Thank you for your time smora! – JF0001 Aug 19 '17 at 14:21

1 Answers1

2

Updating views of a ListView/RecyclerView should not really be done in this way.

First because child views referenced in a ListView do not represent the totality of your rows, but only the visible rows at one moment.

ListView binds their item views on DataSet values, so you'd better use data objects that contains your "checked" boolean status, then when you need to sync, update your DataSet and notifyDataSetChange your adapter.

So you'll have to create your custom adapter by following this kind of example : Custom Adapter for List View

smora
  • 717
  • 5
  • 18
  • Hi smora. I have added the entire onCreate Method. Thank you for your help. Joseph – JF0001 Aug 19 '17 at 14:12
  • Answer to this question is found in smora's last comment from my original post. Thank you again for your help smora. Joseph – JF0001 Aug 19 '17 at 14:23
  • great ;) I've updating my answer with the comment that give you the good way ! – smora Aug 19 '17 at 14:26