Maurício Giordano
Maurício Giordano

Reputation: 3276

OnClickListener on Adapter x OnItemClickListener on ListView

I've always been wondering which one is faster / better to use:

public class SomeAdapter extends BaseAdapter
{

    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater = null;

    public SomeAdapter(Activity a, ArrayList<HashMap<String, String>> d)
    {
        activity = a;
        data = d;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() { return data.size(); }

    public View getItem(int position) { return null; }

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

    static class ViewHolder
    {
        public ImageView someImage;
    }

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

        if(convertView == null)
        {
            convertView = inflater.inflate(R.layout.adapter_some, null);
            holder = new ViewHolder();

            holder.someImage = (ImageView) convertView.findViewById(R.id.someImage);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        HashMap<String, String> events = new HashMap<String, String>();
        events = data.get(position);

        int res = Integer.parseInt(events.get("imageResource"));

        holder.someImage.setImageResource(R.drawable.res);

        convertView.setOnClickListener(new OnClickListener
        {
            @Override
            public void onClick(View v)
            {
                // TODO something here
            }
        });

        return convertView;
    }
}

Should I set OnClickListener on convertView or should I set OnItemClickListener on ListView:

someList = (ListView) ((ViewGroup) rootView.findViewById(R.id.listLayout)).getChildAt(0);

someList.setAdapter(new SomeAdapter(getActivity(), null));

someList.setOnItemClickListener(new OnItemClickListener()
{
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3)
    {
        // TODO something here
    }
});

Thanks

Upvotes: 0

Views: 707

Answers (3)

vinsk
vinsk

Reputation: 61

I doubt there speed should be the decisive factor here. you would not make this choice based on speed,instead the answer lies in the fact the do you have lot of click-able child items in single row of the list view and do you want to work with them individually or in a specific way. If yes you go for it but remember since listview items are recycled you will have to write extra code to track data associated with the list rows.

if there are not many child items (as it appears in your case), setting OnItemClickListener on ListView is the best choice . It has excellent wrapper methods to do most useful stuff and keeps your adapter clean and reusable too.

Upvotes: 1

Chefes
Chefes

Reputation: 1942

The best practices is implementing in the listview, because your adapter can be usefull for another listview. The adapter principal function is just organize, show and adapt the information to some container in this case Listview, never validate functionallity because that code or validation should be in Bussiness Layer.

Upvotes: 1

Ahmad
Ahmad

Reputation: 72533

Should I set OnClickListener on convertView or should I set OnItemClickListener on ListView?

Set an OnItemClickListener on your ListView.

This way you'll set the listener on the whole row. You should only set an OnClickListener on a specific View in your ListViews row, if you have various elements that should be clickable and do something else, than a click event on the whole row.

Upvotes: 2

Related Questions