How to extract the text from the selected item on the listView

后端 未结 14 2483
轻奢々
轻奢々 2020-11-28 07:55

I have a listview with some items. I would like to get the text from the selected item.

Here is my list adapter and the onItemClickListener:

ListView         


        
相关标签:
14条回答
  • 2020-11-28 08:22

    Use this:

    String selectedFromList = (lv.getItemAtPosition(position));
    

    Hope it will help. :)

    0 讨论(0)
  • 2020-11-28 08:26

    This worked for me. I had tried some solutions here but they hadn't worked. Hope this helps.

    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myFamily);
    
        listViewDemo.setAdapter(arrayAdapter);
    
        listViewDemo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    
                //String nameSelectedfromList = arrayAdapter.getItem(position).toString();
                String nameSelectedfromList = listViewDemo.getItemAtPosition(position).toString();
    
                Log.i("Clicked Item", nameSelectedfromList);
    
            }
        }); 
    
    0 讨论(0)
  • 2020-11-28 08:30

    If you are using a List to populate the ListView in your onItemClick() method.

    String s = list.get(position);
    

    You need to be able to access this list within the inner class

    0 讨论(0)
  • 2020-11-28 08:30
    myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> av,
                                    View view, int position, long l) {
                String text = String.valueOf(myListView.getItemAtPosition(position));
    
                }
            }
        });
    
    0 讨论(0)
  • 2020-11-28 08:30

    It is as simple as that

    @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                TextView text1 = (TextView) findViewById(R.id.Text1);
                TextView text2 = (TextView) findViewById(R.id.Text2);
    
                String txt_1 = ""+text1.getText().toString().trim();
                String txt_2 = ""+text2.getText().toString().trim();
    
               //Other_Related_Work
    
            }
        });
    
    0 讨论(0)
  • 2020-11-28 08:32

    try doing it by this, insert it into onItemClickListener, i am not sure what does your Country class look like:

    String s = values.get(position).getCountryName();
    Log.e("LISTVIEW", "selected item text = "+s);
    
      or
    
    String s = values.get(position).toString();
    Log.e("LISTVIEW", "selected item text = "+s);
    
    0 讨论(0)
提交回复
热议问题