通过编程访问特定的行中CheckedTextView的名单 - Android电子 [英] Programatically access specific rows in List of CheckedTextView's - Android

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

问题描述

有可能以编程方式访问特定的行中CheckedTextViews的列表,以改变他们的文本框的状态?

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

我的方案有哪几种CheckedTextViews用户可以preSS切换状态的列表视图。

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,然后保存到IDS数据库的列表。

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方法,到目前为止,我记得从数据库中检查的项目,但我不知道如何进军IDS回到列表视图元素。我可以这样做:

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

在此先感谢

凯文

推荐答案

这是什么伊夫落得这样做..但它似乎是一个完整的黑客攻击。
基本上我必须设置为循环双..一到我的列表中的元素进行迭代,一个通过游标进行迭代,我retreived我检查表状态(简单元素的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天全站免登陆