Question

I have a listview with an image and a textview in each row. I want to get the value of the text on the clicking of the whole row. Below code is behaving strange.I am getting the value of random row instead of the one clicked.

I am not getting what is wrong. Any help would be appreciated.

I am doing this, :

public static class LazyAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 

    public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.list_row, null);

        title = (TextView)vi.findViewById(R.id.title); // title
        ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image

        HashMap<String, String> song = new HashMap<String, String>();
        song = data.get(position);

        // Setting all values in listview
        title.setText(song.get(Meida_listActivity.KEY_TITLE));
        imageLoader.DisplayImage(song.get(Meida_listActivity.KEY_THUMB_URL), thumb_image);


        vi.setOnClickListener( new View.OnClickListener()
        {


            public void onClick(View v)
            {
                  String listValue = (String) title.getText();
                  System.out.println("this is value of string ::: :::: " + listValue);

            }


        });
        return vi;
    }
}
Was it helpful?

Solution

You can also try implementing click for listview and get text like this!Hope it works..

listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            TextView text = (TextView) view.findViewById(R.id.title);
            String lst_txt = text.getText().toString().trim();
            System.out.println("this is value of string ::: :::: " + lst_txt);
        }
    });

OTHER TIPS

On your OnClickListener change your code to this:

  vi.setOnClickListener( new View.OnClickListener()
    {


        public void onClick(View v)
        {
              String listValue = (String) data.get(position).get(Meida_listActivity.KEY_TITLE);
              System.out.println("this is value of string ::: :::: " + listValue);

        }


    });

That way you ensure you are referring the correct row.

type data of variable data is ArrayList

    HashMap<String, String> _hashMap = (HashMap<String, String>) data.get(position);
    String _test = _hashMap.get("Text").toString();
 vi.setOnClickListener( new View.OnClickListener()
    {
   public void onClick(View v)
        {
     title = (TextView)v.findViewById(R.id.title);
              String listValue = title.getText().toString();
              System.out.println("this is value of string ::: :::: " + listValue);

        }


    });
    return vi;

add below line
listView.setOnItemClickListener(this);

and Override onItemClick method. Retrive object on the basis of position(third parameter).

use this listener into Activity oncreate method

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {     

    @Override
    public void onItemClick(AdapterView<?> av, View arg1, int index,
            long arg3) {


    // Perform Action here..... 
String value = av.getItemAtPosition(index).toString();
    }

  });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top