自定义列表视图适配器上的 OnclickListener

OnclickListner on a custom listview adapter

我是 Android 的新手,所以您可能觉得这有点傻,但请帮忙。 我有点喜欢 listviews 类似于 whatsapp 和其他 IM 应用程序 等的人

现在,当我单击 listview 时,我想获得所单击的 listview 的相应 id。 我怎么可能那样做? 我将把 onclicklistner 方法放在哪里?

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


}

您应该将 onClickListener 放在 Activity 中。

  your_list.OnItemClickListener = new MyOnClickListenerClass();

并创建 class

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

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

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

    }
});

在您的 activity 中将适配器设置为列表视图调用后

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

编辑:

要在 ListView 中获取点击的用户详细信息,请使用

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

然后在ListView调用的setOnItemClickListener中

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

希望对您有所帮助。

使用列表视图的setOnItemClickListener()。 您必须在您的 MainActivity class 中使用此方法,其中定义了您的 Listview

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

位置变量将 return 所选项目的 ID。

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