【问题标题】:Programatically access specific rows in List of CheckedTextView's - Android以编程方式访问 CheckedTextView 列表中的特定行 - Android
【发布时间】:2010-08-24 13:15:39
【问题描述】:

是否可以通过编程方式访问 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 但我在这里展示了我的意思

提前致谢

凯文

【问题讨论】:

    标签: android listview list checkedtextview


    【解决方案1】:

    你可以打电话

    listView.setItemChecked(int position, boolean value)
    

    【讨论】:

    • 问题是我没有项目所在的位置,我只有它的 id,这让我想知道是否有一些方法可以访问 listView 项目(在这种情况下是 CheckedTextView)通过它的 id。
    • 大概位置就是适配器中的位置。如果你循环适配器并且当你找到一个选中的项目时,你调用该方法。这只是一个循环。
    • 不是真的,如果我在适配器中有 20 个项目并且我需要检查其中的 10 个,我仍然需要检查适配器中的每个项目与我想要检查的每个项目是否需要嵌套循环
    【解决方案2】:

    这就是我最终做的事情……但这似乎是一个完整的 hack。 基本上我必须设置一个双循环..一个循环遍历我的列表元素,一个循环遍历我已经检索了我的检查列表状态的游标(一个简单的元素 ids 数组,当状态最后一次检查时)保存)

    我的外部 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();
    

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

    谢谢

    凯文

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多