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

三世轮回 提交于 2020-01-05 03:07:14

问题


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


回答1:


You can call

listView.setItemChecked(int position, boolean value)



回答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



来源:https://stackoverflow.com/questions/3556764/programatically-access-specific-rows-in-list-of-checkedtextviews-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!