Toast not generating text from selected item from list

后端 未结 5 2039
抹茶落季
抹茶落季 2021-01-25 17:42

I\'ve made a simple app in android with list View,In that i want to make a toast when select an item,i have tried as below but its not working..

my code is as below:

相关标签:
5条回答
  • 2021-01-25 18:17

    I hope this way get selected item

          lv.setOnItemClickListener(new OnItemClickListener() {
    
                        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                                long arg3) {
                            // TODO Auto-generated method stub
                         //  Toast.makeText(getApplicationContext(),"Position is: "+ position, Toast.LENGTH_LONG).show();
                           String selectedFromList = lv.getItemAtPosition(position).toString();
                            Toast.makeText(getApplicationContext(),selectedFromList , Toast.LENGTH_LONG).show();
                        }
                });
    
    0 讨论(0)
  • 2021-01-25 18:18

    Try

    String selectedValue = from[position];
    
    0 讨论(0)
  • 2021-01-25 18:22
    HashMap<String, String> selectedValue = (HashMap<String, String>) (lv.getItemAtPosition(position));         
    ArrayList<String> list = new ArrayList<String>(selectedValue.keySet());             
    Toast.makeText(getApplicationContext(), selectedValue.get("txt"), Toast.LENGTH_LONG).show();
    

    That hashmap has got keys which are present in that list. That list is actually the from array which you have given. Just give the corresponding key to display the corresponding text.

    Its working. :)

    0 讨论(0)
  • 2021-01-25 18:22

    You can take the adapter and take value from it.

    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) 
                String selectedValue =(String) (lv.getAdapter().getItem(position));
                Toast.makeText(getApplicationContext(),Toast.LENGTH_LONG).show();
            }
    
    0 讨论(0)
  • 2021-01-25 18:38

    I am so confused on what you are doing, why don't you just do it like this:

    String selectedValue = items[position];
    

    instead of:

    String selectedValue =(String) (lv.getItemAtPosition(position));
    
    0 讨论(0)
提交回复
热议问题