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 set onClick event in list item of ListView
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent = new Intent(context, SendMessage.class);
String message = "abcpqr";
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
});
try this way.
How to add an onClickListener to different items in a ListView row
Create your own Adapter
which extends the ArrayAdapter
(see this link), override the getView()
method and in there you can add OnClickListener
s to every View
in the ListView
's child. To notify the ListView
a child is removed, call notifyDataSetChanged()
on the ListView
's Adapter
.
You could solve this using the ListView
, but I would suggest using the RecyclerView
(Android RecyclerView). RecyclerView
has better support for updating its children (like removing, inserting, updating children) and has default animations set.
Adding onClickListener to specific items in ListView
You can define your listView.setOnItemClickListener
like this to go to different activity for clicking different element.
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String item = listView.getItemAtPosition(position);
Toast.makeText(this,"You selected : " + item,Toast.LENGTH_SHORT).show();
if(position==0) {
// Do your code for clicking "Smartphone Plans" example
// startActivity(new Intent(getApplicationContext(),SmartphonePlans.class));
}
else if(position==1) {
// Do your code for clicking "Internet Bundles". example
// startActivity(new Intent(getApplicationContext(),InternetBundles.class));
}
else if(position==2) {
// Do your code for clicking "Weekend Plans". example
//startActivity(new Intent(getApplicationContext(),WeekendPlans.class));*/
}
});
how to attach a onClick Listener to a listview item on an app widget
The section of Adding behavior to individual items in the official doc say:
As described in Using the AppWidgetProvider Class, you normally use setOnClickPendingIntent() to set an object's click behavior—such as to cause a button to launch an Activity. But this approach is not allowed for child views in an individual collection item (to clarify, you could use setOnClickPendingIntent() to set up a global button in the Gmail app widget that launches the app, for example, but not on the individual list items). Instead, to add click behavior to individual items in a collection, you use setOnClickFillInIntent(). This entails setting up up a pending intent template for your collection view, and then setting a fill-in intent on each item in the collection via your RemoteViewsFactory.
Your RemoteViewsFactory must set a fill-in intent on each item in the collection. This makes it possible to distinguish the individual on-click action of a given item. The fill-in intent is then combined with the PendingIntent template in order to determine the final intent that will be executed when the item is clicked.
Your need to call setOnClickFillInIntent
to differentiate the item on-click behavior. Something like this:
public RemoteViews getViewAt(int position) {
final RemoteViews remoteView = new RemoteViews(
context.getPackageName(), R.layout.widget_item);
[...]
// Next, set a fill-intent, which will be used to fill in the pending intent template
// that is set on the collection view in StackWidgetProvider.
Bundle extras = new Bundle();
extras.putInt(YouWidgetProvider.EXTRA_ITEM, position);
Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
// Make it possible to distinguish the individual on-click
// action of a given item
remoteView.setOnClickFillInIntent(R.id.item_frame, fillInIntent);
return remoteView;
}
Also setPendingIntentTemplate
should be use instead of setOnClickPendingIntent
to set a single PendingIntent
template on the collection. Set the pending intent in onUpdate
method corresponding to you subclass of AppWidgetProvider
, something like this:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// update each of the app widgets with the remote adapter
for (int i = 0; i < appWidgetIds.length; ++i) {
...
RemoteViews remoteView = new RemoteViews(context.getPackageName(),
R.layout.widget_item);
...
// This section makes it possible for items to have individualized behavior.
// It does this by setting up a pending intent template. Individuals items of a collection
// cannot set up their own pending intents. Instead, the collection as a whole sets
// up a pending intent template, and the individual items set a fillInIntent
// to create unique behavior on an item-by-item basis.
Intent activityIntent = new Intent(context, ListActivity.class);
// Set the action for the intent.
// When the user touches a particular view.
activityIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
activityIntent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i],
activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteView.setPendingIntentTemplate(R.id.stack_view, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteView);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
Android : How to set onClick event for Button in List item of ListView
You can set the onClick
event in your custom adapter's getView
method..
check the link http://androidforbeginners.blogspot.it/2010/03/clicking-buttons-in-listview-row.html
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);
is it possible to set onClickListener for custom listView item in activity?
1. Update your customButtonListener
interface as below:
public interface customButtonListener {
public void onEditButtonClickListner(int position, String value);
public void onDeleteButtonClickListner(int position);
}
2. In adapters getView()
method set click listener to edit
and delete
buttons:
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull final ViewGroup parent) {
............
...................
// Edit
viewHolder.editBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (customListner != null) {
customListner.onEditButtonClickListner(position, getItem(position).getExerciseName());
}
}
});
// Delete
viewHolder.deleteBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (customListner != null) {
customListner.onDeleteButtonClickListner(position);
}
}
});
return convertView;
}
3. In your activity, add CustomButtonListener
to your ListView
:
A. Get item position from `onEditButtonClickListner()` and get `workoutExercise` object and pass it to another activity
B. Get item position from `onDeleteButtonClickListner()` and delete item and upadte ListView.
Add below codes in your Activity:
..........
.................
listAdapter.setCustomButtonListener(create_workout.this);
exercisesList.setAdapter(listAdapter);
exercisesList.setCustomButtonListener(new WorkoutExerciseListAdapter.customButtonListener() {
@Override
public void onEditButtonClickListner(int position, String value)
{
// Item
WorkoutExercise workoutExercise = workoutExercises.get(position);
// Do something with object workoutExercise
}
@Override
public void onDeleteButtonClickListner(int position)
{
// Delete
workoutExercises.remove(position);
// Update ListView
listAdapter.notifyDataSetChanged();
}
});
Hope this will help~
Related Topics
Close Application and Remove from Recent Apps/
Finish Parent and Current Activity in Android
How to Add an Image in Email Body
Get Listview Item Position on Button Click
Autocompletetextview with Custom Adapter and Filter
Android Google Maps, How to Make Each Marker Infowindow Open Different Activity
How to Open Standard Google Map Application from My Application
Any Way to Link to the Android Notification Settings for My App
Generate Signed APK Android Studio
Camera Tutorial for Android (Using Surfaceview)
Android: Get Height of a View Before It's Drawn
How to Cancel an Dialog Themed Like Activity When Touched Outside the Window
Why Does Alertdialog.Builder(Context Context) Only Accepts Activity as a Parameter
Cmusphinx Pocketsphinx - Recognize All (Or Large Amount) of Words
How to Use Holo.Light Theme, and Fall Back to 'Light' on Pre-Honeycomb Devices