Slim Shady
Slim Shady

Reputation: 1085

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;
    }


}

Upvotes: 1

Views: 786

Answers (5)

Nagaraju V
Nagaraju V

Reputation: 2757

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.

Upvotes: 1

programmer23
programmer23

Reputation: 543

the_list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //Use this if you want to get the whole ListView id
            parent.getId();

            //or use this if you want the item id
             view.getId()
        }
    });

Upvotes: 2

Shree
Shree

Reputation: 795

Use setOnItemClickListener() of listview. You have to use this method in Your MainActivity class where your Listview is defined

 listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long   id) {
String item = contests_listView.getItemAtPosition(position);
Toast.makeText(this,"You selected : " + item,Toast.LENGTH_SHORT).show();                
         }
      });

the position variable will return id of selected item.

Upvotes: 2

Karol Żygłowicz
Karol Żygłowicz

Reputation: 2452

There is another method for the listView: onItemClickListener

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View item,
            int position, long id) {
          //this code will be fired after clicking on listview item

    }
});

Upvotes: 1

Naumand
Naumand

Reputation: 71

You should put the onClickListener in the Activity.

  your_list.OnItemClickListener = new MyOnClickListenerClass();

and create also the class

 public class MyOnClickListenerClass: Java.Lang.Object, AdapterView.IOnItemClickListener
{

    public void OnItemClick(AdapterView parent, View view, int position, long id)
    {
       //do stuf 
    }
}    

Upvotes: 1

Related Questions