获取ListView中行的特定项

时间:2017-12-13 07:58:16

标签: android listview onitemclick

我制作了一个ListView,它有一些行。在ItemClickListener我想要访问单击该行的哪个项目

lstcontact.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            String v=lstcontact.getItemAtPosition(position).toString();
            if(view.getId()==R.id.btncall) {
                Toast.makeText(MainActivity.this, "Make A Call at " + position, Toast.LENGTH_SHORT).show();
            }else if(view.getId()==R.id.btnmess) {
                Toast.makeText(MainActivity.this, "Make A Message at " + position, Toast.LENGTH_SHORT).show();
            } 
        }


    });

我想在用户点击消息或呼叫按钮时显示祝酒词

2 个答案:

答案 0 :(得分:0)

您没有为该行中的项目调用onclick。为此,您需要为适配器实现侦听器。

public interface CustomListener{
    void onSomeItemClick(View view);
}

在适配器构造函数中,您需要初始化此侦听器;

public YourAdapter(Context context, CustomListener listener, List<YourClass> list){
    this.context = context;
    this.listener = listener;
    this.list = list;
 }

在您创建此适配器的活动中,您需要实现侦听器

adapter = new YourAdapter(this,this,list);

之后你需要覆盖onSomeItemClick方法

@Override 
public void someItemClick(View view){
    if(view.getId()==R.id.btncall) {
        Toast.makeText(MainActivity.this, "MAke A Call at "+position, Toast.LENGTH_SHORT).show();
    }else if(view.getId()==R.id.btnmess) {
        Toast.makeText(MainActivity.this, "MAke A Message at "+position, Toast.LENGTH_SHORT).show();
    } 
}

在您的适配器getView()中,您需要在要查看的视图中实现onClickListener

view.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if(listener!=null{
            listener.onSomeItemClick(v);
        }       
    }
});

答案 1 :(得分:0)

onItemClick中的view参数是整个ListView行(包含所有按钮和所有内容),因此view.getId()== R.id.btncall是没有意义的。

您最好在自定义适配器中按钮的onClickListener中设置Toasts。很高兴看到您的布局/适配器代码,但基本的自定义适配器(使用ViewHolder模式,效率)可能看起来像这样:

public class MyListAdapter extends ArrayAdapter<YourItemType> {

static class ViewHolder {
    private Button callButton;
    private Button messageButton;
    // declare any other views you want to control here
}


public MyListAdapter(Context context, List<YourItemType> items) {
    super(context,0,items);
}

@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.your_listview_layout, null);
        holder = new ViewHolder();
        holder.callButton = (Button) convertView.findViewById(R.id.btncall);
        holder.messageButton = (Button) convertView.findViewById(R.id.btnmess);
        // find views for any other views in your ViewHolder / Listview layout
        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }
    YourItemType item = getItem(position);
    holder.callButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // simple say what position is
            Toast.makeText(getContext(), "Make A Call at "+String.valueOf(position), Toast.LENGTH_SHORT).show();
            // do something with the item itself
            Toast.makeText(getContext(), "This toast is for info on the item, which is  "+ item.toString(), Toast.LENGTH_SHORT).show();

        }
    });
    holder.messageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // simple say what position is
            Toast.makeText(getContext(), "Make A Call at "+String.valueOf(position), Toast.LENGTH_SHORT).show();
            // do something with the item itself
            Toast.makeText(getContext(), "This toast is for info on the item, which is  "+ item.toString(), Toast.LENGTH_SHORT).show();
        }
    });
    return convertView;
}

}