Help Regarding Onclick() Event on an Item of Listview Custom Row Layout

Help regarding onClick() event on an item of ListView custom row layout

Please refer this,

Me just writing the code to give you idea, Not in correct format

 class youaddaper extends BaseAdapter{

public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflate = LayoutInflater.from(context);
View v = inflate.inflate(id, parent, false);

ImageView imageview = (ImageView) v.findViewById(R.id.imageView);
imageview.setOnClickListener(new imageViewClickListener(position));
//you can pass what ever to this class you want,
//i mean, you can use array(postion) as per the logic you need to implement
}
class imageViewClickListener implements OnClickListener {
int position;
public imageViewClickListener( int pos)
{
this.position = pos;
}

public void onClick(View v) {
{// you can write the code what happens for the that click and
// you will get the selected row index in position
}
}

}

Hope it helped you

set button onclick event for every row of listview

Use a CustomAdapter.

You need to understand how listview works

How ListView's recycling mechanism works

Pass the activity context and the list NewItems to the constructor of the custom adapter.

   @Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
pDialog.dismiss();
list=(ListView)findViewById(R.id.listupdate);
CustomAdapter cus = new CustomAdapter(MainActivtiy.this,newItemlist);
list.setAdapter(cus);

}

Use a Custom Layout with textviews and buttons. Name it list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="33dp"
android:layout_marginTop="40dp"
android:text="TextView" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView1"
android:layout_centerHorizontal="true"
android:text="TextView" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView2"
android:layout_marginLeft="34dp"
android:layout_toRightOf="@+id/textView2"
android:text="TextView" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView3"
android:layout_marginTop="20dp"
android:text="Button" />

</RelativeLayout>

Inflate the layout, initialize and update the views. Set Listener on the button do what is required.

  public class CustomAdapter extends BaseAdapter
{
LayoutInflater mInlfater;
ArrayList<HashMap<String,String>> list;
public CustomAdapter(Context context,ArrayList<HashMap<String,String>> list)
{
mInlfater = LayoutInflater.from(context);
this.list =list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if(convertView ==null)
{
convertView = mInlfater.inflate(R.layout.list_item,false);
holder = new ViewHolder();
holder.b1 = (Button)convertView.findViewById(R.id.button1);
holder.tv1 = (TextView)convertView.findViewById(R.id.textView1);
holder.tv2 = (TextView)convertView.findViewById(R.id.textView2);
holder.tv3 = (TextView)convertView.findViewById(R.id.textView3);
convertView.setTag(holder);

}
else
{
holder =(ViewHolder) convertView.getTag();
}
HashMap<String,String> map = list.get(position);
holder.tv1.setText(map.get("name"));
holder.tv2.setText(map.get("description"));
holder.tv3.setText(map.get("price"));
holder.b1.setOnClickListener(new OnClickListener()
{

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

}

});
return convertView;
}
static class ViewHolder
{
Button b1;
TextView tv1,tv2,tv3;
}
}

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

Android onclick Event on custom ListView adapter not working

I'm guessing your Adapter holds objects of type Person. Since the fields in your Person class is public, you can access them like this.

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
{
// Get Person "behind" the clicked item
Person p = (Person) myListView.getItemAtPosition(position);

// Log the fields to check if we got the info we want
Log.i("SomeTag", "Persons name: " + p.name);
Log.i("SomeTag", "Persons id : " + p.person_id);

// Do something with the data. For example, passing them to a new Activity

Intent i = new Intent(MainActivity.this, NewActivity.class);
i.putExtra("person_id", p.person_id);
i.putExtra("person_name", p.name);

MainActivity.this.startActivity(i);
}

OnClick Button action in Custom ListView

Add your button's OnClick event in the Pro_Adapter's getView() methond as you do normally in your activities' onCreate() method.

Setting click listener on button in Listview row layout

Try making these changes to CustomOutboxListAdapter class.

In CustomOutboxListAdapter don't implement View.OnClickListener. Change it's class declaration from:

public class CustomOutboxListAdapter extends BaseAdapter implements View.OnClickListener{

to:

public class CustomOutboxListAdapter extends BaseAdapter {

And remove the onClick method inside:

@Override
public void onClick(View view) {

Replace public View getView(int position, View convertView, ViewGroup parent) with:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;

if(convertView == null){
LayoutInflater inf = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.listview_outbox_messages_row, null);

//private inner class used to minimize the calls to "findViewById" method
holder = new ViewHolder();
holder.datetimeText = convertView.findViewById(R.id.outbox_msgSentTime);
holder.messageText = convertView.findViewById(R.id.outbox_msgText);
holder.receiverUsernameText = convertView.findViewById(R.id.outbox_msgReceiverUsername);

convertView.setTag(holder);
}
else {
holder = (ViewHolder)convertView.getTag();
}

SentMessageTemplate stu = sentMsgsList.get(position);
holder.datetimeText.setText(stu.getTimestamp());
holder.messageText.setText(stu.getMessageContent());
holder.receiverUsernameText.setText(stu.getMsgReceiver());

// use local variable so it is always referenced correctly
Button deleteBtn = convertView.findViewById(R.id.btn_deleteMsg_SMF);
// Cache row position inside the button using `setTag`
deleteBtn.setTag(position);

// Attach the click event handler
deleteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = (Integer) view.getTag();

// Access the row position here to get the correct data item
SentMessageTemplate sentMessageTemplate = getItem(position);

String receiverUsername = sentMessageTemplate.getTimestamp();
String timestamp = sentMessageTemplate.getMsgReceiver();

sentMsgsFragment.deleteSentMsg(receiverUsername, timestamp);
}
});

return convertView;
}

Just delete the member variable private Button btnDeleteMsg; as not used anymore.

Also for getItemId(int i) implementation, I think it should be something like:

@Override
public long getItemId( int position ) {
return this.sentMsgsList.get(position).getId();
}

Then in SentMessageTemplate class, it should implement:

public int getId() {
// return unique ID for object
....
}

Assign onClick event for Button in list item of ListView

You have

 View row = inflater.inflate(R.layout.addtooutlet_list_item, parent,
false);

But you return

return convertView;

Should be

View convertView = inflater.inflate(R.layout.addtooutlet_list_item, parent,
false);

And

 Button buttonEdit = (Button) convertView.findViewById(R.id.item_button);

And

return convertView;

Edit:

As suggested in the comment by blackbelt. You are not using Custom ArrayAdapter

You probably meant

Myadapter adapter = new MyAdapter(ActivityName.this,R.layout.addtooutlet_list_item,mylist);
setListAdapter(adapter);

Then

 ArrayList<HashMap<String,String> map;
LayoutInflater mInflater;
public Myadapter(Context context, int resource, int textViewResourceId,
ArrayLsit<HashMap<String,String> map, OnClickListener callback) {
super(context, resource, textViewResourceId, map);
this.map = map;
mInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder;
if(convertView ==null)
{
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.addtooutlet_list_item, parent,
false);
holder.tv1 = (TextView) convertView.findViewById(R.id.textView1);
holder.tv2 = (TextView) convertView.findViewById(R.id.textView2);
holder.b = (Button) convertView.findViewById(R.id.item_button);
convertView.setTaf(holder);
}else{
holder =(ViewHolder) convertView.getTag();
}
HashMap<String,String> hm = map.get(position);
holder.tv1.setText(hm.get(postion).get("bus_name").toString());
holder.tv2.setText(hm.get(postion).get("bus_id").toString());
holder.b.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Log.i("xx", "Button pressed!");
}
});

return convertView;
}

The ViewHodler

static class ViewHolder
{
Button b;
TextView tv1,tv2;
}

Now you should have a textview's in layout.addtooutlet_list_item.xml and update views in getView.

Also consider using a ViewHolder Pattern

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

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



Leave a reply



Submit