Adding an Onclicklistener to Listview (Android)

Adding an onclicklistener to listview (android)

listView.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object o = prestListView.getItemAtPosition(position);
prestationEco str = (prestationEco)o; //As you are using Default String Adapter
Toast.makeText(getBaseContext(),str.getTitle(),Toast.LENGTH_SHORT).show();
}
});

How to set OnClickListener in Listview to send to another activity

add this in CuacaNowActivity.java

listViewKab.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {

Intent intent = new Intent(CurrentActivity.this, TargetActivity.class);

startActivity(intent);
}
});

EDIT

First add item in res/menu/main.xml file to show in menu i am adding settings here

ex:

main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/menu_settings"/>

</menu>

then in your activity code:

 //it will show the menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

//it will open the new activity (Settings in my case) when selected
@Override
public boolean onOptionsItemSelected(MenuItem item) {

boolean ret;
try {

if (item.getItemId() == R.id.menu_settings) {
// Handle Settings
Intent intent = new Intent(CurrentActivity.this, Settings.class);
startActivity(intent);

ret = true;

} catch (Exception e) {
}
ret = super.onOptionsItemSelected(item);
return ret;

}

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 OnClickListeners 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));*/
}

});

Android Adding OnClickListener to listview

On PreferenceActivity listView is hiddent behind getListView();
The simpliest example:

ListView listView = getListView();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> av, View view, int i, long l) {
Toast.makeText(Activity.this, "myPos "+i, Toast.LENGTH_LONG).show();
}
});

Code will look like

package fr.xgouchet.tuto.switchpreferences;

import java.util.ArrayList;
import java.util.List;

import android.preference.PreferenceActivity;
import android.widget.ListAdapter;

public class MyPrefsActivity extends PreferenceActivity {

private List<Header> mHeaders;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

ListView listView = getListView();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> av, View view, int i, long l) {
Toast.makeText(Activity.this, "myPos "+i, Toast.LENGTH_LONG).show();
}
});
}

protected void onResume() {
super.onResume();

setTitle("Settings");

if (getListAdapter() instanceof MyPrefsHeaderAdapter)
((MyPrefsHeaderAdapter) getListAdapter()).resume();
}

protected void onPause() {
super.onPause();
if (getListAdapter() instanceof MyPrefsHeaderAdapter)
((MyPrefsHeaderAdapter) getListAdapter()).pause();
}

public void onBuildHeaders(List<Header> target) {
// Called when the settings screen is up for the first time
// we load the headers from our xml description

loadHeadersFromResource(R.xml.my_prefs_headers, target);

mHeaders = target;
}

public void setListAdapter(ListAdapter adapter) {
int i, count;

if (mHeaders == null) {
mHeaders = new ArrayList<Header>();
// When the saved state provides the list of headers,
// onBuildHeaders is not called
// so we build it from the adapter given, then use our own adapter

count = adapter.getCount();
for (i = 0; i < count; ++i)
mHeaders.add((Header) adapter.getItem(i));
}

super.setListAdapter(new MyPrefsHeaderAdapter(this, mHeaders));
}

}

How to set onClickListener for custom ListView

you could use setonitemclicklistener

 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
imageList.get(position); // here you will get the clicked item from
//your imagelist and you can check by getting a title by using this

String title= imageList.get(position).getTitle();
if(title.equals("you title to match")){
//do your action or you can get a particular position and click there
}
}
});

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~

How can i add a setOnClickListener to my ListView Android

How can i add a setOnClickListener to my ListView Android

You should implement setOnItemClickListener to your possessionList listView

possessionList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// your code
}
});

Another alternative would be add android:descendantFocusability="blocksDescendants" in the LinearLayout or RelativeLayout, not inside listView element.

Example

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
.....
android:descendantFocusability="blocksDescendants"
tools:context="de.control.overview">

Add an onclicklistener to every row from a listview (android)

You can add onItemClickListener just like below

listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

Toast.makeText(this,"You selected : " + position,Toast.LENGTH_SHORT).show();
}
});

Add onClickListener to listview item

You can use mStrings and mImages in your OnItemClickListener. Assumed from your LazyAdapter that they are arrays. Maybe you can try something like this.

list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String text = mStrings[position];
YourImageClass img = mImages[position];
Intent i = new Intent(MainActivity.this, ShowFullImageActivity.class);
i.putExtra("TEXT", text);
i.putExtra("IMAGE", img); // <-- Assumed you image is Parcelable
startActivity(i);
}
}


Related Topics



Leave a reply



Submit