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

2

7

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

Reggy answered 28/12, 2013 at 10:39 Comment(3)
Post your custom adapter code,is the listView having more than one textView?Agreed
what do you get from listView? means Task,Priority etc..Refund
@piyush gupta: First, I want whole text of list view item and after that i will split that text with '\n'Reggy
S
9
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();

            }
        });
Simard answered 28/12, 2013 at 10:55 Comment(5)
Hi rAviNder : which method should i access from object v ? Should i use getTag() ?Reggy
You can use getTag() if you are setting view's tag with the value you want to capture. Refer for same developer.android.com/training/improving-layouts/…Simard
I have done like this String xyz=(String) selectedView.getTag();in onClick of "Fix", but i am getting blank string xyz. Is this written in right way ? or i need to do any chages ?Reggy
i have edited my question. Now i have posted Adapter code in latest edit.Reggy
I have updated the answer. You need to find text view from list view item and then access the text value. But still you are not using ViewHolder pattern my recommendation is that go through the tutorial and use it. It will make list view faster.Simard
C
1

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.

Concessive answered 15/1, 2015 at 13:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.