rlc
rlc

Reputation: 6069

Listening to Click on ListView with a Custom Adapter

I am developing an Android app, which retrieves some images from the internet and put them on a ListView. When one of the images is clicked, another activity is created with some details about the image. The problem is that I am not being able to track the clicks on the list.

Activity.java

public class MyActivity extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        // List<FlickerPicture> pictures = ...
        ListView listView = (ListView) findViewById(android.R.id.list);
        listView.setOnItemClickListener(new OnItemClickListener() {
             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                 Log.d("MyActivity", "You clicked on " + arg2);
                 Toast.makeText(getBaseContext(), "You clicked on " + arg2,
                 Toast.LENGTH_LONG).show();
             }
         });
        ArrayAdapter<FlickerPicture> adapter = new FlickrItemAdapter(this, pictures);
        listView.setAdapter(adapter);
    }
}

FlickrItemAdapter

public class FlickrItemAdapter extends ArrayAdapter<FlickerPicture> {
    public FlickrItemAdapter(Activity activity, List<FlickerPicture> pictures) {
        super(activity, 0, pictures);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Activity activity = (Activity) getContext();
        LayoutInflater inflater = activity.getLayoutInflater();
        // Inflate the views from XML
        View rowView = inflater.inflate(R.layout.miniature_layout, null);
        FlickerPicture picture = getItem(position);
        // Load the image and set it on the ImageView
        ImageButton imageButton = (ImageButton) rowView.findViewById(R.id.miniature_picture);
        try {
            imageButton.setImageBitmap(Util.loadBitmap(picture.getMiniatureLink()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return rowView;
    }
}

Question

How do I do to make the images on my list "clickable"?

Upvotes: 2

Views: 777

Answers (1)

Sam
Sam

Reputation: 86948

You are using an ImageButton not an ImageView. Simply change miniature_layout.xml and your adapter's getView() to use an ImageView, because the ImageButton is consuming the click event preventing it from reaching the ListView.

Upvotes: 1

Related Questions