Question

is it possible to programatically access specific rows in a list of CheckedTextViews to change the state of their textboxes?

my program has a listview which has several CheckedTextViews which the user can press to toggle state.

I want to save the state of the checkboxes when the user leaves the activity, so I have in my onPause method:

public void onPause(){
         super.onPause();
         SparseBooleanArray positions;
         positions = listView.getCheckedItemPositions();
         ListAdapter items = listView.getAdapter();
         int j = items.getCount();

         ArrayList<Long> ids = new ArrayList<Long>();
         for (int k =0; k < j;k++){
             if(positions.get(k)==true){
                 ids.add(items.getItemId(k));   
             }
         } 
         this.application.getServicesHelper().open();
         this.application.getServicesHelper().storeServices(ids,visit_id);
         this.application.getServicesHelper().close();
     }

which very simply iterates the list view, adds the checked items to an ArrayList and then saves that list of ids to the database.

My problem lise in trying to reset the list once a user goes back to that activity.

so far in my onStart method, I recall the checked items from the database, but I do not know how to march the ids returned to the listview elements. can I do something like:

listView.getElementById(id_from_database).setChecked?

I know I cant use getElementById but I have it here to show what I mean

Thanks in advance

Kevin

Was it helpful?

Solution 2

This is what Ive ended up doing.. but it seems like a complete hack. Basically I have to set up a double for loop.. one to iterate through my list elements, and one to iterate through the cursor that I have retreived my check list state (a simply array of ids of elements that were checked when state was last saved)

my outer for iterates through the list elements checking each id against a loop through the list of ids to be set as checked. if they equal each other then set that item as checked.

    // mAdapter is contains the list of elements I want to display in my list. 
ServiceList.this.setListAdapter(mAdapter);

        // Getting a list of element Ids that had been previously checked by the user. getState is a function I have defined in my ServicesAdapter file.

    Cursor state = ServiceList.this.application.getServicesHelper().getState(visit_id);
    int checks = state.getCount();
    int check_service;               
    int c = mAdapter.getCount();
    if(checks>0){
        state.moveToFirst(); 
        for (int i=0; i<checks; i++) { 
            // set check_service = the next id to be checked
            check_service = state.getInt(0);
            for(int p=0;p<c;p++){

                if(mAdapter.getItemId(p)==check_service){
                        // we have found an id that needs to be checked. 'p' corresponds to its position in my listView
                    listView.setItemChecked(p,true);
                    break;
                }
            }
            state.moveToNext(); 
        } 
    }
    ServiceList.this.application.getServicesHelper().close();

Please tell me there is a more efficient way of achieving this!!

Thanks

Kevin

OTHER TIPS

You can call

listView.setItemChecked(int position, boolean value)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top