How to handle ListView click in Android
On your list view, use setOnItemClickListener
How to handle the click event in Listview in android?
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);
ListView with OnItemClickListener
Though a very old question, but I am still posting an answer to it so that it may help some one.
If you are using any layout inside the list view then use ...
android:descendantFocusability="blocksDescendants"
... on the first parent layout inside the list. This works as magic the click will not be consumed by any element inside the list but will directly go to the list item.
Handling on item click in Widget ListView [Android]
I found a great example I tried following it and changes how the data is set and then on the onReceive
in my AppWidgetProvider
I call there my ActivityOne
and passed the data from what I get in the Bundle
. This link also help me to start an Activity
inside the onReceive
inside the AppWidgetProvider
. I'm just encountering app crashes when removing the widget on home screen. Will update this answer when I found out how to solve it.
Edit
I got to solve the crashing part I forgot to add the onDelete
and then add notifyAppWidgetViewDataChanged
as well for the ListView
its working fine now.
Android ListView with onClick items
In your activity, where you defined your listview
you write
listview.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?>adapter,View v, int position){
ItemClicked item = adapter.getItemAtPosition(position);
Intent intent = new Intent(Activity.this,destinationActivity.class);
//based on item add info to intent
startActivity(intent);
}
});
in your adapter's getItem you write
public ItemClicked getItem(int position){
return items.get(position);
}
Android - How to create clickable listview?
In fact it is quite easy:
This is your Activity with the ListView, it implements an OnItemClickListener:
public class MainActivity extends Activity implements OnItemClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
//* *EDIT* *
ListView listview = (ListView) findViewById(R.id.listView1);
listview.setOnItemClickListener(this);
}
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
Log.i("HelloListView", "You clicked Item: " + id + " at position:" + position);
// Then you start a new Activity via Intent
Intent intent = new Intent();
intent.setClass(this, ListItemDetail.class);
intent.putExtra("position", position);
// Or / And
intent.putExtra("id", id);
startActivity(intent);
}
Edit
The above code would be placed in your MainActivity.java. I changed the name of the class to MainActivity
and the contentView to setContentView(R.layout.activity_main)
- The names are those of a freshly created Android Project in Eclipse.
Please see also the 2 new lines under //* Edit * - those will set the Listener for clicks on items in the list.
Your activity_main.xml should look like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:entries="@array/sections" >
</ListView>
</RelativeLayout>
The array.xml (not string.xml) in your `res/values/` folder looks like this
<resources>
<string-array name="sections">
<item >Pro Constructive</item>
<item >Con Constructive</item>
<item >1st Speaker Cross</item>
<item >Pro Rebbutal</item>
<item >Con Rebuttal</item>
<item >2nd Speaker Cross</item>
<item >Pro Summary</item>
<item >Con Summary</item>
<item >Grand Cross</item>
<item >Pro Final Focus</item>
<item >Con Final Focus</item>
</string-array>
</resources>
N.B.: If you copy & paste this code it should work. But you will get an error by clicking on an Item because you haven't created the ListItemDetail.class
yet.
Here is an example of how this could look:
Your ListItemDetail.java:
public class ListItemDetail extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listitem);
Intent intent = getIntent();
int position = intent.getIntExtra("position", 0);
// Here we turn your string.xml in an array
String[] myKeys = getResources().getStringArray(R.array.sections);
TextView myTextView = (TextView) findViewById(R.id.my_textview);
myTextView.setText(myKeys[position]);
}
}
And its activity_listitem.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/my_textview"/>
</LinearLayout>
If you copy past this code it will work.
Related Topics
How to Startforeground() Without Showing Notification
How to Call a .Net Webservice from Android Using Ksoap2
Android Turn On/Off Wifi Hotspot Programmatically
How to Create a File in Android
Creating a Button in Android Toolbar
Difference Between Android.App.Fragment and Android.Support.V4.App.Fragment
Getting the Physical Screen Dimensions/Dpi/Pixel Density in Chrome on Android
How to Get String Width on Android
Android Dynamically Change Style at Runtime
Make Certain Area of Bitmap Transparent on Touch
How to Calculate Distance Between Two Locations Using Their Longitude and Latitude Value
Call Removeview() on the Child's Parent First
How to Enable Google Play App Signing
How to Find and Run the Keytool
Android Asynctask for Long Running Operations