Syed Sehu Mujammil A
Syed Sehu Mujammil A

Reputation: 875

Android onClick for ImageButton inside ListView and get position

I Have a list view in my android application which has a ImageButton in it. I need to write a action for my ImageButton onClick. I'm able to write a onClick for my ImageButton with a listener but I'm not able to get the position of the current ListItem that the user clicked on.

I Tried different solutions to get the position but none of them worked.

Here is my custom adapter file

package com.fyshadows.collegemate;

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

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ForumAdapter extends ArrayAdapter<DiscussionList> {
private static List<DiscussionList> items = null;

public ForumAdapter(Context context, List<DiscussionList> items) {
    super(context, R.layout.custom_list, items);
    this.items = items;
}

@Override
public int getCount() {
    return items.size();
}

public static DiscussionList getModelPosition(int position) {
    return items.get(position);
}

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

    if (v == null) {
        LayoutInflater li = LayoutInflater.from(getContext());
        v = li.inflate(R.layout.custom_list, null);
    }

    DiscussionList app = items.get(position);

    if (app != null) {
        TextView titleText = (TextView) v.findViewById(R.id.dscTitle);
        TextView categoryText = (TextView) v.findViewById(R.id.dscCategory);
        TextView descriptionText = (TextView) v
                .findViewById(R.id.dscDescription);
        TextView timeText = (TextView) v.findViewById(R.id.dscTime);
        TextView idText = (TextView) v.findViewById(R.id.dscDiscId);

        titleText.setText(app.getTitle());
        categoryText.setText(app.getCategory());
        descriptionText.setText(app.getDescription());
        timeText.setText(app.getTime());
        idText.setText(app.getDiscId());

        String dId = app.getDiscId();

        //onClick for image button inside list view

        ImageButton imgButton = (ImageButton) v
                .findViewById(R.id.imageButton1);
        imgButton.setFocusable(false);  
        imgButton.setTag(position);
        imgButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final int position = getView(position, view, parent).get(position);
                Toast.makeText(getContext(), position,
                        Toast.LENGTH_SHORT).show();
            }
        });

    }


    return v;
}
}

I'm able to show a toast with some dummy string but can't get the position for some reason.

Thanks in advance

And now the error log shows

06-20 01:46:43.498: E/AndroidRuntime(12672):    at android.content.res.Resources.getText(Resources.java:246)
06-20 01:46:43.498: E/AndroidRuntime(12672):    at android.widget.Toast.makeText(Toast.java:265)
06-20 01:46:43.498: E/AndroidRuntime(12672):    at com.fyshadows.collegemate.ForumAdapter$1.onClick(ForumAdapter.java:72)
06-20 01:46:43.498: E/AndroidRuntime(12672):    at android.view.View.performClick(View.java:4421)
06-20 01:46:43.498: E/AndroidRuntime(12672):    at android.view.View$PerformClick.run(View.java:17903)
06-20 01:46:43.498: E/AndroidRuntime(12672):    at android.os.Handler.handleCallback(Handler.java:730)
06-20 01:46:43.498: E/AndroidRuntime(12672):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-20 01:46:43.498: E/AndroidRuntime(12672):    at android.os.Looper.loop(Looper.java:213)
06-20 01:46:43.498: E/AndroidRuntime(12672):    at android.app.ActivityThread.main(ActivityThread.java:5225)
06-20 01:46:43.498: E/AndroidRuntime(12672):    at java.lang.reflect.Method.invokeNative(Native Method)
06-20 01:46:43.498: E/AndroidRuntime(12672):    at java.lang.reflect.Method.invoke(Method.java:525)
06-20 01:46:43.498: E/AndroidRuntime(12672):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
06-20 01:46:43.498: E/AndroidRuntime(12672):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
06-20 01:46:43.498: E/AndroidRuntime(12672):    at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 1726

Answers (1)

codeMagic
codeMagic

Reputation: 44571

You are using setTag() but never doing anything with it. Change

final int position = getView(position, view, parent).get(position);

to

final int position = (Integer)view.getTag();

right now it is probably giving you the position of the last item that was iterated through getView().

Edit

Your Toast needs a String so you will need to change it to

Toast.makeText(getContext(), "" + position, Toast.LENGTH_SHORT).show();

or convert it into a String before.

Upvotes: 1

Related Questions