【问题标题】:Set ListView checkmark as checked/unchecked by program in Android ListActivity将 ListView 复选标记设置为由 Android ListActivity 中的程序选中/取消选中
【发布时间】:2012-06-09 15:11:35
【问题描述】:

我为我的 Android 应用程序引用了几个示例。在ListActivity中,在OnCreate方法之前,items数组被预定义为

String[] items = new String[]{"Text for Item1", "text for item2", ....};

在OnCreate方法内部,我用最简单的方式设置了适配器,并在下面显示列表视图:

setListAdapter( new ArrayAdapter<String>(this,
 android.R.layout.simple_list_item_checked, items));

我已经重写了方法:

@Override    
 protected void onListItemClick(ListView l, View v, int position, long id)    
{     
     CheckedTextView textview = (CheckedTextView)v;
     textview.setChecked(!textview.isChecked());
} 

以上所有代码都可以正常工作。 ListView中每个项目的复选标记可以显示和手动设置选中/取消选中。

我的问题是: 我想通过程序设置一些项目,而不是通过手动单击来选中/取消选中,并且选中标记也随之更改。可以做到吗?怎么做?

感谢提前的帮助

【问题讨论】:

    标签: android android-listview


    【解决方案1】:

    我认为在this 中说的 Google 的 Android 工程师 Romain Guy 可以解决您的问题:

    Actually you want to use CheckedTextView with choiceMode. That's what
    CheckedTextView is for. However, you should not be calling setChecked
    from bindView(), but let ListView handle it. The problem was that you
    were doing ListView's job a second time. You don't need listeners
    (click on onlistitem), calls to setChecked, etc.
    

    这是我的解决方案:

    class MyActivity extends ListActivity { // or ListFragment
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // some initialize
    
            new UpdateCheckedTask().execute(); // call after setListAdapter
        }
    
        // some implementation
    
        class UpdateChecked extends AsyncTask<Void, Void, List<Integer>> {
    
            @Override
            protected List<Integer> doInBackground(Void... params) {
                ListAdapter listAdapter = getListAdapter();
                if (listAdapter == null) {
                    return null;
                }
    
                List<Integer> positionList = new ArrayList<Integer>();
                for (int position = 0; position < listAdapter.getCount(); position++) {
                    Item item = (Cursor) listAdapter.getItem(position); // or cursor, depends on your ListAdapter implementaiton
                    boolean checked = item.isChecked() // your model
                    positionList.add(position, checked);
                }
                return positionList;
            }
    
            @Override
            protected void onPostExecute(List<Integer> result) { // setItemChecked in UI thread
                if (result == null) {
                    return;
                }
    
                ListView listView = getListView();
                for (Iterator<Integer> iterator = result.iterator(); iterator.hasNext();) {
                    Integer position = iterator.next();
                    listView.setItemChecked(position, true);
                }
            }
        }
    }
    

    【讨论】:

    • 谢谢@Gasol!但是我无法遵循解决方案。由于我在 ListView 上使用的方法 setListAdapter() 是最简单的方法。要更改为在提供的 AbsListView 小部件中使用 setItemChecked() 方法,可能会使我的应用程序更加复杂。这是唯一的方法吗?
    • @cmh ListAdapter 中没有办法做到这一点,请参阅我更新的答案。
    • 谢谢@Gasol!稍后会尝试解决方案,看看是否能达到预期。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-14
    • 1970-01-01
    • 1970-01-01
    • 2014-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多