Android - how to add an item click method to an ArrayAdapter

前端 未结 3 1253
Happy的楠姐
Happy的楠姐 2021-01-12 03:11

I have a simple ArrayAdapter. I want to set up a listener for every row click of my list such that a new Activity opens. How would I do that? My ArrayAdapter code -

3条回答
  •  悲哀的现实
    2021-01-12 03:42

    Assuming you are using a ListActivity implementing OnItemClickListener you could use this code:

    ArrayAdapter ad = new ArrayAdapter(this,
                    android.R.layout.simple_list_item_checked, items);
            setListAdapter(ad);
            ListView list = getListView();
            list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
            //list.setItemChecked(0, true);
            list.setOnItemClickListener(this);
    
    
    

    EDIT: Otherwise, if you don't extend ListActivity, have a listview in your layout and replace ListView list = getListView() with something like ListView list = findViewById(R.id.listView). Replace list.setOnItemClickListener(this) with

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView parent, View view, int position, long id) {
    
                }
            });
    

    提交回复
    热议问题