0

I have a Listview with custom adapter using ViewHolder pattern... It Works fine, but now I added two buttons for each row in the listview in my layout.

When the button is clicked, I want to get the position (for get data), I impletemented OnClickListener

How can I do this?

This is part of my code:

public class SearchListAdapter extends BaseAdapter implements OnClickListener {

public static final String TAG = "SearchListAdapter";

private LayoutInflater inflater;
private ArrayList<VideoYoutube> arrayList;
private ImageLoader imageLoader;
private Activity activity;

public SearchListAdapter(Activity a, ArrayList<VideoYoutube> array){

    activity = a;
    arrayList = array;
    inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader = new ImageLoader(activity.getApplicationContext());
}


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

@Override
public VideoYoutube getItem(int position) {
    return arrayList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

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

    View vi = convertView;
    ViewHolder holder = null;

    //Check if it's on memory
    if(vi == null) {
        //The view is not a recycled one: we have to inflate
        vi = inflater.inflate(R.layout.list_row_search, parent, false);

        holder = new ViewHolder();
        holder.video_title = (TextView)vi.findViewById(R.id.title);
            holder.button_play = (Button)vi.findViewById(R.id.button_play);
        holder.button_show = (Button)vi.findViewById(R.id.button_show);

        holder.button_play.setOnClickListener(this);
        holder.button_show.setOnClickListener(this);

        vi.setTag(holder);
    }
    else {

        // View recycled !
        // no need to inflate
        // no need to findViews by id
        holder = (ViewHolder) vi.getTag();
    }

    Object o = getItem(position);

    //Set video information
    holder.video_title.setText(o.getYtTitle());

    return vi;
}


@Override
public void onClick(View v) {
    switch(v.getId()){
    case R.id.button_play:
        Log.d(TAG, "Play: " + v.getId());
        break;
    case R.id.button_show:
        Log.d(TAG, "Show: " + v.getId());
        break;
    }
}   

}

limaho33
  • 25
  • 7

2 Answers2

1

As I understood, you can use this:

private ListView mListView;

//Method Constructor
mListView = l;

@Override
public void onClick(View v) {

    int position;

    switch(v.getId()){
    case R.id.button_play:
        position = mListView.getPositionForView((View) v.getParent());
        Log.d(TAG, "Clicked: " + position);
//search in your arrayList with the position that you'll get

        break;
    }
}
deleon
  • 63
  • 4
0

You can set tag for buttons with needed data, like item id or position. And then you can get what you need in click listener, like this:

public View getView(int position, View convertView, ViewGroup parent) {

    ...

    Object o = getItem(position);

    //Set video information
    holder.video_title.setText(o.getYtTitle());

    holder.button_play.setTag(position);
    holder.button_show.setTag(position);

    return vi;
}

...

@Override
public void onClick(View v) {
    switch(v.getId()){
    case R.id.button_play:
        Object o = getItem((int) v.getTag())
        Log.d(TAG, "Play: " + o.getYtTitle());
        break;
    case R.id.button_show:
        Object o = getItem((int) v.getTag())
        Log.d(TAG, "Show: " + o.getYtTitle());
        break;
    }
} 
senneco
  • 1,786
  • 2
  • 12
  • 15