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:
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();
}
});
Try
String selectedValue = from[position];
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. :)
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();
}
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));