简体   繁体   中英

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

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).

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

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).

I want the normal text in the string 'str'. (ie text of selected item in listview). OR in other words, I want the same text as listview item in String 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 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:

在此输入图像描述

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

在此输入图像描述

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

            }
        });

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 arg1 by 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 ).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM