Let's say that I have a ListView with some items. I want to make it so when the user clicks on an item, the app pops up a Toast containing the name of the item. For example, when the user clicks "Apple", they are presented with the toast: "You ate Apple." How can I do so?
4 Answers
1 Comment
Arielle
But how to get the name of the pressed item?
you can set Item Click Listener on List View i.e.
listvw=(ListView)findViewById(R.id.listviewid);
listvw.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
Toast.makeText(ActivityName.this, "Text message", Toast.LENGTH_SHORT).show();
}
});
3 Comments
Arielle
Same here. How to get the name of the pressed item?
Mukesh Kumar Singh
You have a view of your listview item. just find the text of view i.e. ' TextView text= (TextView)view.findViewById(R.id.text_viewId); ' 'Toast.makeText(ActivityName.this, text.getText(), Toast.LENGTH_SHORT).show();'
Arielle
OK thanks. I used
TextView text = (TextView) arg1.findViewById(arg1.getId()); Toast.makeText(getApplicationContext(), text.getText(), Toast.LENGTH.SHORT).show();use like that
ActivityListView .java
package com.sunil;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class ActivityListView extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an array of Strings, that will be put to our ListActivity
String[] namesArray = new String[] { "Linux", "Windows7", "Eclipse",
"Suse", "Ubuntu", "Solaris", "Android", "iPhone" };
/* Create an ArrayAdapter, that will actually make the Strings above
appear in the ListView */
this.setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, namesArray));
}
@Override
protected void onListItemClick(ListView l, View v,
int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
Toast.makeText(this, "You selected: " + keyword,
Toast.LENGTH_SHORT).show();
}
}
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
2 Comments
Arielle
I get a
java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'. Maybe let's extend the standard Activity class, not ListActivity?Sunil Kumar
same code working in my device. Do not extends Activity and also setcontentview also . It will work fine check again
Try this code listview=(ListView)findViewById(R.id.listid);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,long arg3) {
// to set your list item or name
//here list is your list that you set in your adapter
list.get(pos);
}
});