Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'. (i.e 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:

enter image description here

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

enter image description here

like image 628
Rohhit Avatar asked Dec 28 '13 10:12

Rohhit


People also ask

Which method is used to obtain the selected option from a ListView?

To get which item was selected, there is a method of the ListView called getItemAtPosition.

What is the difference between dialog and AlertDialog?

AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .


Video Answer


2 Answers

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

            }
        });
like image 179
AndRSoid Avatar answered Oct 08 '22 13:10

AndRSoid


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.

like image 38
Tolar Avatar answered Oct 08 '22 11:10

Tolar