Within a ListView, why do I keep getting the value of the first row instead of the selected row?

后端 未结 2 1182
醉酒成梦
醉酒成梦 2021-01-27 12:29

In my android app I have the following code:

public void goToCaptureBonus (View View) {
        String tappedBonus = ((TextView) findViewById(R.id.bonusListCode)         


        
2条回答
  •  执念已碎
    2021-01-27 12:52

    In case of ListView you should use OnItemClickListener. Here is an example how to do this.

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {
            //use the position variable to get the list item from the list
            String selection = itemsArray[position]; // this will give the selected item 
        }
    });
    

    Now whenever an item on your ListView is tapped, the method onItemClick() is called. And here you can get the selection.

提交回复
热议问题