14

我有一个带有两个按钮的自定义 ListView,当我单击任何行上的任一按钮时,我想在 Listview 上获取文本标签,现在只需弹出一个 toast 即可。到目前为止,没有任何效果,我一直在获取数组中的最后一项。

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

在此处输入图像描述

这是我的自定义 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();
4

5 回答 5

14

容易做到:

    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(); 
        } 
    }); 
于 2012-06-22T12:40:50.077 回答
5

使用 View 的 setTag 属性............

作为

Button call = (Button) rowView.findViewById(R.id.button1);
call.setTag(position);

call.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
               int which = -1;
               Obejct obj =  v.getTag();
              if(obj instaceof Integer){
               which  = ((Integer)obj).intValue();
                    }

              if(which >-1){
                String name = values[which];
                Toast.makeText(CustomListView.this, name, Toast.LENGTH_SHORT).show();
                }
            }
        });
于 2012-06-22T12:36:14.913 回答
2

如果您有一个 ListActivity,并且您没有使用自己的适配器,您仍然可以获得属于点击按钮的列表项,如下所示:

在列表行的布局文件中:

<ImageButton
        android:id="@+id/button_call"
        android:layout_height="48dip"
        android:layout_width="48dip"
        android:contentDescription="Call"
        android:onClick="callBuddy"
        android:src="@drawable/call_button_image"
        />

在您的 ListActivity 中:

public void callBuddy(View view) {

    int position = getListView().getPositionForView((View) view.getParent());
    Buddy buddyToCall = (Buddy) getListView().getItemAtPosition(position);

    Toast.makeText(MyListActivity.this, String.format("Calling your buddy %s.", buddyToCall.name), Toast.LENGTH_SHORT).show();

}
于 2013-04-23T17:25:34.640 回答
0

将 onClick="click" 设置为按钮/图像/等的 xml...

在您的活动中,执行以下操作:

public void click(View v) {
    final int position = getListView().getPositionForView(v);
    String text = getListView().getItemAtPosition(position).toString();
    Toast.makeText(getApplicationContext, text, Toast.LENGTH_SHORT).show();
}
于 2013-07-15T23:12:26.830 回答
0

只需使用 getItem() 并传递位置 例如:getItem(position).getID() 这里的 getID() 方法是 getter 方法

于 2017-07-06T05:59:58.157 回答