M.A.Murali
M.A.Murali

Reputation: 10158

how to perform action for list view in android

in my app i use list view. in the list view i have three image buttons(play,detail,buy). each image button has individual actions. how can i perform onclick action for each image button in list view.

my code:

public class AndroidThumbnailList extends ListActivity{
      ..........
   public class MyThumbnaildapter extends ArrayAdapter<String>{
      public MyThumbnaildapter(Context context, int textViewResourceId,String[] objects) {
       super(context, textViewResourceId, objects);
            // TODO Auto-generated constructor stub
       }
      public View getView(int position, View convertView, ViewGroup parent) {
           .........
      }
   }


   public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
    _contentUri = MEDIA_EXTERNAL_CONTENT_URI;
    initVideosId();
  setListAdapter(new MyThumbnaildapter(AndroidThumbnailList.this, R.layout.row, _videosId));
  }



}

how to write action for my list view. please help me.

Upvotes: 1

Views: 941

Answers (1)

sksamuel
sksamuel

Reputation: 16387

You will need to write your own Adapter that inflates the view you want to use, and then assigns an OnClick listener to each of the images. Here is some sample code from one of my projects that does something similar (but with a single checkbox that I add a listener to).

public class GroupListAdapter extends BaseAdapter {

private List<Group> groups;

// ... constructors here

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

@Override
public Group getItem(int position) {
    return groups.get(position);
}

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

@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
    final Group group = getItem(position);

    final View view;
    if (convertView == null)
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.group, null);
    else
        view = convertView;

    view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // do stuff when the rest of the view is clicked
        }
    });

    TextView tv = (TextView) view.findViewById(R.id.group_name);
    tv.setText(group.getName());

    final CheckBox check = (CheckBox) view.findViewById(R.id.group_checkbox);
    check.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // do stuff when clicked
        }
    });


    return view;
}

}

Upvotes: 2

Related Questions