Android - ListView - performItemClick

前端 未结 24 1161
清歌不尽
清歌不尽 2020-11-29 03:10

I\'m facing some difficults when I try to use the performItemClick funcion of the ListView.

All I want to do is to perform a click programatically i

相关标签:
24条回答
  • 2020-11-29 03:44

    Dropping Some Experience.

    using listview1.performItemClick, will also trigger your listview1.OnItemClickListener if you are using the listener with same listview in your code.

    Hope It helps  
    
    0 讨论(0)
  • 2020-11-29 03:45

    this may be old but this may help :

    lvList.performItemClick(null, index, lvList.getItemIdAtPosition(index) ); 
    

    NOTE : the first param is null and will still work, if you have a custom adapter, convertView will be filled with custom layout and view and such.

    -cheers / happy codings.

    0 讨论(0)
  • 2020-11-29 03:46

    Using the code @sulal proposed, you may place it in onLoadFinished, if you use a LoaderManager. Eg something like

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        //....
        // mSelectedId keeps the currently selected id
        // INVID is an invalid value
        if (mSelectedId == INVID) { // nothing selected
            // sulal's code
            new Handler().post(new Runnable() {
                @Override
                public void run() {
                    mList.performItemClick(
                            mList.getChildAt(mActivePosition),
                            mActivePosition,
                            mList.getAdapter().getItemId(mActivePosition));
                    mSelectedId = mList.getAdapter().getItemId(mActivePosition);
                }
            });            
        }
    

    mActivePosition may be 0 (ie position on the first item) or a position kept during eg onPause

    0 讨论(0)
  • 2020-11-29 03:46

    This worked for me:

    listView.getAdapter().getView(1, null, null).performClick();
    
    0 讨论(0)
  • 2020-11-29 03:47

    This is from Begining Android Games. It creates a simple list of items which you can click to open a new activity. Each list item of course, would have to also be added to the AndroidManifest.xml as a separate activity with a .ListItem# name.

    public class MainActivity extends ListActivity {
    String tests[] = { "ListItem1",
                       "ListItem2",
                       "ListItem3",
                       "ListItem4"};
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, tests));
    }
    
    @Override
    protected void onListItemClick(ListView list, View view, int position, long id) {
        super.onListItemClick(list, view, position, id);
        String testName = tests[position];
    
        try {
            Class<?> classInstance = Class.forName("your.package.name." + testName);
            Intent intent = new Intent(this, classInstance);
            startActivity(intent);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    

    }

    0 讨论(0)
  • 2020-11-29 03:48

    I went with

    listView.getAdapter().getView(position, null, null).performClick();
    
    0 讨论(0)
提交回复
热议问题