Android:从自定义列表视图中点击的按钮获取列表视图项

我有一个带有两个按钮的自定义ListView,当我点击任意一行上的任一按钮时,我想在Listview上获取文本标签,现在只需弹出一个吐司.到目前为止,我还没有收到我的数组中的最后一个项目.

这是一个屏幕截图,让您更好地了解我的意思

这是我的自定义ListView的适配器子类

static final String[] Names = 
           new String[] { "John", "Mike", "Maria", "Miguel"};


class MyArrayAdapter extends ArrayAdapter<String> {
    private final Context context;      

    int which;

    public MyArrayAdapter(Context context, String[] pValues) {
        super(context, R.layout.main, pValues);
        this.context = context;
        values = pValues;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        rowView = inflater.inflate(R.layout.main, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
        Button call = (Button) rowView.findViewById(R.id.button1);
        Button chat = (Button) rowView.findViewById(R.id.button2);
        textView.setText(values[position]);


        // Change icon based on name
        s = values[position];           
        which = position;
        call.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String name = values[which];
                Toast.makeText(CustomListView.this, name, Toast.LENGTH_SHORT).show();
            }
        });

        return rowView;
    }
}

编辑:

String name = textView.getText().toString();
RelativeLayout ll = (RelativeLayout)v.getParent();
textView = (TextView)ll.findViewById(R.id.label);
Toast.makeText(CustomListView.this, name,
Toast.LENGTH_SHORT).show();
最佳答案
容易做:

    call.setOnClickListener(new OnClickListener() { 
        @Override 
        public void onClick(View v) { 
            RelativeLayout rl = (RelativeLayout)v.getParent();
            TextView tv = (TextView)rl.findViewById(R.id.label);
            String text = tv.getText().toString();
            Toast.makeText(CustomListView.this, text, Toast.LENGTH_SHORT).show(); 
        } 
    }); 

转载注明原文:Android:从自定义列表视图中点击的按钮获取列表视图项 - 代码日志