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;
}
}
}