Android 单击AlertDialog按钮时如何获取列表视图所选项目文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20813567/
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 03:30:58  来源:igfitidea点击:

how to get listview selected item text when clicked on AlertDialog button

androidandroid-listview

提问by Rohhit

I am developing a simple application in android. In this app i am having a listview, which shows the inbox sms. On click of any item of listview, one AlertDialog appears, that contains Ignore,Cancel, & Fix buttons.(Please refer images for this).

我正在android中开发一个简单的应用程序。在这个应用程序中,我有一个列表视图,它显示收件箱短信。单击列表视图的任何项目时,会出现一个 AlertDialog,其中包含忽略、取消和修复按钮。(请参阅图片)。

On click of "Fix" button of AlertDailog, I want to get the text of selected item of listview.

单击 AlertDailog 的“修复”按钮时,我想获取列表视图所选项目的文本。

using following code, I am getting the text in String 'str' is like this:- com.example.myapp.Items@412a7d08(In log as well as on toast).

使用以下代码,我得到字符串“str”中的文本是这样的:- com.example.myapp.Items@412a7d08(在日志中以及在吐司上)。

I want the normal text in the string 'str'. (i.e text of selected item in listview). OR in other words, I want the same text as listview item in String str.

我想要字符串“str”中的普通文本。(即列表视图中所选项目的文本)。或者换句话说,我想要与字符串 str 中的列表视图项相同的文本。

Can anybody help me to solve this issue ?

有人可以帮我解决这个问题吗?

This is the code i have written :

这是我写的代码:

TextView tv_view_task;
ListView lv_view_task;
static String sms = "";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_task);
    tv_view_task=(TextView) findViewById(R.id.tv_view_task);
    lv_view_task=(ListView) findViewById(R.id.lv_view_task);

    //List<String> msgList = getSms();
   MyAdapter adapter=new MyAdapter(this,getSms());
   lv_view_task.setAdapter(adapter);

   lv_view_task.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> arg0, View arg1,final int arg2,
            long arg3) {
        // TODO Auto-generated method stub


        AlertDialog.Builder alert=new AlertDialog.Builder(ViewTask.this);
        alert.setTitle("What you want to do ?");
        alert.setIcon(R.drawable.ic_launcher);

        alert.setPositiveButton("Fix",new OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                String str=lv_view_task.getItemAtPosition(arg2).toString();
                Toast.makeText(getApplicationContext(), str,Toast.LENGTH_LONG).show();


            }
        });
        alert.setNegativeButton("Ignore",new OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

            }
        });
        alert.setNeutralButton("cancel",new OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                dialog.cancel();
            }
        });
        AlertDialog ale=alert.create();
        ale.show();
    }
});
}

My Adapter code is :

我的适配器代码是:

package com.example.myapp;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class MyAdapter extends ArrayAdapter<Items> 
{

 private final Context context;
 private final ArrayList<Items> itemsArrayList;
public MyAdapter(Context context,ArrayList<Items>itemsArrayList) {
    super(context, R.layout.view_task_list_view,itemsArrayList);
    // TODO Auto-generated constructor stub
    this.context=context;
    this.itemsArrayList=itemsArrayList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    LayoutInflater inflator=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView=inflator.inflate(R.layout.view_task_list_view,parent,false);
    TextView label=(TextView) rowView.findViewById(R.id.label);
    //TextView view_task_priority=(TextView) rowView.findViewById(R.id.view_task_tv_priority);
    label.setText(itemsArrayList.get(position).getTitle());
    //view_task_priority.setText(itemsArrayList.get(position).getPriority());
    return rowView;
}

}

XML for custom listview...:

用于自定义列表视图的 XML...:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

 <TextView
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="30px"
    android:paddingLeft="5dp"
    android:text="title"
    android:textColor="@color/Black">
</TextView>

Images : 1) ListView:

图像:1)列表视图:

enter image description here

在此处输入图片说明

2) AlertDialog : Here user clicks on "Fix" to get selected item text of listView.

2) AlertDialog : 这里用户点击“Fix”来获取listView 的选中项文本。

enter image description here

在此处输入图片说明

回答by AndRSoid

lv_view_task.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> arg0, View arg1,final int arg2,
            long arg3) {
        final View selectedView v = arg1 ; // Save selected view in final variable**

        AlertDialog.Builder alert=new AlertDialog.Builder(ViewTask.this);
        alert.setTitle("What you want to do ?");
        alert.setIcon(R.drawable.ic_launcher);

        alert.setPositiveButton("Fix",new OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                String str=lv_view_task.getItemAtPosition(arg2).toString();
                Toast.makeText(getApplicationContext(), str,Toast.LENGTH_LONG).show();
                //Access view object v here
                TextView label=(TextView) v.findViewById(R.id.label);
                String xyz = label.getText();

            }
        });

回答by Tolar

in the method

在方法中

onItemClick(AdapterView<?> arg0, View arg1,final int arg2,
            long arg3){

String txt = ((TextView)arg1).getText().toString();

}

You can access the text of the view arg1by casting it to TextView, which grants you access to the method getText()then you can do with that text whatever you want(of course only when the view is a textView).

您可以arg1通过将其转换为 来访问视图的文本TextView,这将授予您访问该方法的权限,getText()然后您可以随心所欲地使用该文本(当然只有当视图为 a 时textView)。

well.. the question is quite old.. so i guess this is some sort of gravedigging, but my answer seems to not have been here yet.

好吧..这个问题很老..所以我想这是某种挖坟,但我的答案似乎还没有出现。