Search code examples
androidandroid-listviewandroid-arrayadapteronitemclicklistener

How to set onClick for custom array adapter with extra data


I have a custom array adapter for a ListView. The goal is to have each list item have an individual picture, and that when the user clicks on the item they are taken to a web page. I have the image part working, and there are some stackoverflow answers for where to put the onItemClickListener. It seems it would go in the custom array adapter class But I can't figure out how to access the url from the list item.

Here is the code:

public class Animal
{
    int image_resource_id;
    String animal_type;
    String wikipedia_url;

    Animal(int image_resource_id, String animal_type, String wikipedia_url)
    {
        this.image_resource_id = image_resource_id;
        this.animal_type = animal_type;
        this.wikipedia_url = wikipedia_url;
    }
}


class AnimalViewHolder
{
    ImageView animal_image = null;
    TextView animal_name = null;
    String animal_url = "";
}


class CustomAnimalAdapter extends ArrayAdapter<Animal>
{
    Context context;
    int layoutResourceId;   
    Animal data[] = null;

    public CustomAnimalAdapter(Context context, int layoutResourceId, Animal[] data)
    {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        AnimalViewHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new AnimalViewHolder();
            holder.animal_image = (ImageView)row.findViewById(R.id.animal_image);
            holder.animal_name = (TextView)row.findViewById(R.id.animal_name);

            row.setTag(holder);

        }
        else
        {
            holder = (AnimalViewHolder)row.getTag();
        }

        Animal animal = data[position];
        holder.animal_name.setText(animal.animal_type);
        holder.animal_image.setImageResource(animal.image_resource_id);

        return row;
    }

}


protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_main);  

    Animal[] animal_list = {
            new Animal(R.drawable.lion, "Lion", "http://en.wikipedia.org/wiki/Lion"),
            new Animal(R.drawable.tiger, "Tiger", "http://en.wikipedia.org/wiki/Tiger"),
            new Animal(R.drawable.bear, "Bear", "http://en.wikipedia.org/wiki/Bear"),
            new Animal(R.drawable.monkey, "Monkey", "http://en.wikipedia.org/wiki/Monkey"),
            new Animal(R.drawable.moose, "Moose", "http://en.wikipedia.org/wiki/Moose"),
            new Animal(R.drawable.shark, "Shark", "http://en.wikipedia.org/wiki/Shark")
    };

    CustomAnimalAdapter adapter = new CustomAnimalAdapter(this,
            R.layout.row_item, animal_list);

    list_view_1 = (ListView)findViewById(R.id.listView1);

    list_view_1.setAdapter(adapter);

}

Solution

  • update your adapter getview with

     row.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(animal.wikipedia_url));
                   context.startActivity(browserIntent);
                }
            });