以编程方式访问CheckedTextView列表中的特定行-Android [英] Programatically access specific rows in List of CheckedTextView's - Android

查看:45
本文介绍了以编程方式访问CheckedTextView列表中的特定行-Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过编程方式访问CheckedTextViews列表中的特定行以更改其文本框的状态?

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

我的程序有一个列表视图,其中包含几个CheckedTextViews,用户可以按此按钮切换状态.

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

我想在用户离开活动时保存复选框的状态,所以我的onPause方法中有:

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

非常简单地迭代列表视图,将选中的项添加到ArrayList中,然后将该ID列表保存到数据库中.

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.

到目前为止,在我的onStart方法中,我从数据库中调用了选中的项目,但是我不知道如何处理返回到listview元素的id.我可以做类似的事情吗?

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 ?

我知道我不能使用getElementById,但是我在这里显示了我的意思

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

预先感谢

凯文

推荐答案

这是Ive最终要做的..但似乎是一个完整的技巧.基本上,我必须设置一个double for循环.一个循环访问我的列表元素,另一个循环访问我恢复了检查列表状态的游标(状态最后一次被检查的元素ID的简单数组)保存)

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)

我的外部用于遍历列表元素,以循环检查要设置为选中状态的ID列表中的每个ID.如果它们彼此相等,则将该项目设置为选中状态.

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!!

谢谢

凯文

这篇关于以编程方式访问CheckedTextView列表中的特定行-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆