以编程方式访问 CheckedTextView 列表中的特定行 - Android

发布于 2024-09-15 10:00:04 字数 1156 浏览 5 评论 0原文

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

我的程序有一个列表视图,其中有几个 CheckedTextView,用户可以按这些视图来切换状态。

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

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 列表保存到数据库。

我的问题在于一旦用户返回该活动就尝试重置列表。

到目前为止,在我的 onStart 方法中,我记得从数据库中检查过的项目,但我不知道如何将返回的 id 传递给 listview 元素。我可以做类似的事情:

listView.getElementById(id_from_database).setChecked吗?

我知道我不能使用 getElementById 但我在这里用它来展示我的意思

提前致

谢凯文

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱冒险 2024-09-22 10:00:04

您可以致电

listView.setItemChecked(int position, boolean value)

You can call

listView.setItemChecked(int position, boolean value)
浅笑依然 2024-09-22 10:00:04

这就是我最终所做的……但这看起来像是一个完整的黑客。
基本上我必须设置一个双for循环..一个循环遍历我的列表元素,一个循环遍历我已检索检查列表状态的光标(状态最后一次检查时检查的元素的id的简单数组)保存)

我的外部 for 迭代列表元素,检查每个 id 与要设置为检查的 id 列表的循环。如果它们彼此相等,则将该项目设置为选中。

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

请告诉我有更有效的方法来实现这一目标!

谢谢

凯文

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文