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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11156078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:11:55  来源:igfitidea点击:

Android: Get the listview item from button clicked in custom listview

androidarrayslistviewviewandroid-arrayadapter

提问by Amanni

I have a custom ListView with two button and I when I click either button on any row I want to get the text label on the Listview and for now just popup a toast with it. So far nothing has worked I keep getting the last item in my array.

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

Here is a screen shot to give you a better idea of what i mean

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

enter image description here

在此处输入图片说明

Here is my Adapter subclass for my custom ListView

这是我的自定义 ListView 的 Adapter 子类

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

Edit:

编辑:

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

回答by Barak

Easy to do:

容易做到:

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

回答by Dheeresh Singh

use setTag attribute of the View..............

使用视图的 setTag 属性......................

as

作为

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

and

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

回答by Ciske Boekelo

If you have a ListActivity, and you're not using your own adapter, you can still get the list item belonging to the tapped button, like so:

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

In your layout file of the list row:

在列表行的布局文件中:

<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"
        />

In your ListActivity:

在您的 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();

}

回答by Alexandre Prazeres

Set onClick="click" to xml of button/image/etc...

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

and in your Activity, do:

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

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

回答by Ramesh kumar

simply use getItem() and pass the position Ex:getItem(position).getID()here getID() method is getter method

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