Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle the click event in Listview in android?

The below is my testing code to create the list view, the list view display successfully, however, there is error in click event. I would like to create an intent to send a hardcode message to an new activity. However, it show error for the line

Intent intent = new Intent(context, SendMessage.class); 

So , the problem is , what should I provide for this class?

Also , instead of hard code the output message, how to capture the data in list view row and pass to the new activity? e.g. BBB,AAA,R.drawable.tab1_hdpi for the first row.

Thanks.

public class MainActivity extends Activity {     public final static String EXTRA_MESSAGE = "com.example.ListViewTest.MESSAGE";      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         ArrayList<ListEntry> members = new ArrayList<ListEntry>();          members.add(new ListEntry("BBB","AAA",R.drawable.tab1_hdpi));         members.add(new ListEntry("ccc","ddd",R.drawable.tab2_hdpi));         members.add(new ListEntry("assa","cxv",R.drawable.tab3_hdpi));         members.add(new ListEntry("BcxsadvBB","AcxdxvAA"));         members.add(new ListEntry("BcxvadsBB","AcxzvAA"));         members.add(new ListEntry("BcxvBB","AcxvAA"));         members.add(new ListEntry("BvBB","AcxsvAA"));         members.add(new ListEntry("BcxvBB","AcxsvzAA"));         members.add(new ListEntry("Bcxadv","AcsxvAA"));         members.add(new ListEntry("BcxcxB","AcxsvAA"));         ListView lv = (ListView)findViewById(R.id.listView1);         Log.i("testTag","before start adapter");         StringArrayAdapter ad = new StringArrayAdapter (members,this);         Log.i("testTag","after start adapter");         Log.i("testTag","set adapter");         lv.setAdapter(ad);         lv.setOnItemClickListener(new OnItemClickListener() {             @Override             public void onItemClick(AdapterView<?> parent, View view, int position,                     long id) {                 Intent intent = new Intent(context, SendMessage.class);                 String message = "abc";                 intent.putExtra(EXTRA_MESSAGE, message);                 startActivity(intent);             }         });     } 
like image 935
user782104 Avatar asked Jul 25 '13 07:07

user782104


People also ask

How to get click event on ListView in android?

This example demonstrates how do I handle the click event in ListView in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How can you react to click events on an item of a ListView?

One potential way to achieve is it set your ListView to height="wrap_content" If you do that then you can have override onTouchEvent() for your activity to get the events from when the user touches the empty space.

Which is better ListView or RecyclerView?

Simple answer: You should use RecyclerView in a situation where you want to show a lot of items, and the number of them is dynamic. ListView should only be used when the number of items is always the same and is limited to the screen size.

How do you populate a ListView?

You need to do it through an ArrayAdapter which will adapt your ArrayList (or any other collection) to your items in your layout (ListView, Spinner etc.). This is what the Android developer guide says: A ListAdapter that manages a ListView backed by an array of arbitrary objects.


1 Answers

I can not see where do you declare context. For the purpose of the intent creation you can use MainActivity.this

 lv.setOnItemClickListener(new OnItemClickListener() {             @Override             public void onItemClick(AdapterView<?> parent, View view, int position,                     long id) {                 Intent intent = new Intent(MainActivity.this, SendMessage.class);                 String message = "abc";                 intent.putExtra(EXTRA_MESSAGE, message);                 startActivity(intent);             }         }); 

To retrieve the object upon you have clicked you can use the AdapterView:

ListEntry entry = (ListEntry) parent.getItemAtPosition(position); 
like image 125
Blackbelt Avatar answered Sep 22 '22 12:09

Blackbelt