English 中文(简体)
设置 ListView 列表检查标记为 Android List Actionivity 中的程序检查/未检查
原标题:Set ListView checkmark as checked/unchecked by program in Android ListActivity

例如,我为我的安有机器人应用程序参考了几个例子。在列表活动中,在 < 强/ 强 > 方法之前的 < 强/ 强 > 方法之前,“ 强/强” 数组被预先定义为:

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

在 < 坚固 > 创建 < / 坚固 > 方法中,我使用最简单的方法设置适配器,并显示以下列表视图:

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

上述所有代码都正常工作。 ListSView 中每个诉讼人的检查标记可以手动显示和设置检查/未检查。

MY PROBLEM IS: I want to set some items by program,not by hand clicking, to be checked/unchecked and the checkmark be changed along too. Can it be done and how to do it ?

感谢前台的帮助

问题回答

我认为Google的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);
            }
        }
    }
}




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签