Search code examples
androidandroid-listviewandroid-viewonclicklistenerbaseadapter

OnclickListner on a custom listview adapter


I am newbie to Android so you can expect this a little silly but please do help. I have somewhat like custom listviews of people similar to whatsapp and other IM apps etc.

Now when i click the listview i want to get the respective id of the clicked listview. How can i possibly do that ? Where will i put the onclicklistner method ?

CustomListAdapter.java

package com.example.soc.adater;

import com.example.soc.R;
import com.example.soc.model.IMList;
import com.example.soc.util.ImageLoadTask;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class CustomListAdapter extends BaseAdapter {
    private Activity activity;
    private LayoutInflater inflater;
    private List<IMList> listItems;
    //ImageLoader imageLoader = AppController.getInstance().getImageLoader();

    public CustomListAdapter(Activity activity, List<IMList> listItems) {
        this.activity = activity;
        this.listItems = listItems;
    }

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

    @Override
    public Object getItem(int location) {
        return listItems.get(location);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.list_row, null);

        //if (imageLoader == null)
            //imageLoader = AppController.getInstance().getImageLoader();
        ImageView pp = (ImageView) convertView
                .findViewById(R.id.pp);
        TextView pname = (TextView) convertView.findViewById(R.id.pname);
        TextView uname = (TextView) convertView.findViewById(R.id.uname);
        TextView msgcon = (TextView) convertView.findViewById(R.id.msgcon);
        TextView msgtime = (TextView) convertView.findViewById(R.id.msgtime);

        // getting movie data for the row
        IMList m = listItems.get(position);

        // thumbnail image
        //pp.setImageUrl(m.getppUrl(), imageLoader);
         new ImageLoadTask(m.getppUrl(), pp).execute();

        // name
        pname.setText(m.getpname());

        // @username
        uname.setText(String.valueOf(m.getuname()));

        // msgcontent
        msgcon.setText(m.getmsgcon());

        // messageTime
        msgtime.setText(String.valueOf(m.getmsgtime()));        
        return convertView;
    }


}

Solution

  • In your activity after setting adapter to listview call

    yourListView.setOnItemClickListener(new OnItemClickListener() {
    
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
                // TODO Auto-generated method stub
                //based on position you can get id
            }
        });
    

    EDIT :

    To get clicked user details in ListView use

    convertView.setTag(m) in getView() of CustomAdapter
    

    then in setOnItemClickListener of ListView call

    IMList obj=(IMList)arg1.getTag();
    

    Hope this will helps you.