Programatically access specific rows in List of CheckedTextView's - Android
Asked Answered
B

2

2

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

Bicameral answered 24/8, 2010 at 13:15 Comment(0)
B
1

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

Bicameral answered 24/8, 2010 at 16:4 Comment(0)
G
1

You can call

listView.setItemChecked(int position, boolean value)
Gottfried answered 24/8, 2010 at 14:0 Comment(3)
The thing is I dont have the postion that teh item is at, I only have its id, which left me wondering if there is some method that can access a listView Item( in this case a CheckedTextView) by its id.Bicameral
Probably the position is the position in the adapter. If you loop the adapter and when you find a checked item, you call the method. This is one loop only.Gottfried
Not really, if I have 20 items in the adapter and I need 10 of them to be checked I will still have to check EACH item in the adapter against EACH item I want check which will require a nested loopBicameral
B
1

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

Bicameral answered 24/8, 2010 at 16:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.