Listview : Getting the text of the selected row

2019-09-20 05:50发布

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;
    }
}

6条回答
Luminary・发光体
2楼-- · 2019-09-20 05:56

type data of variable data is ArrayList

    HashMap<String, String> _hashMap = (HashMap<String, String>) data.get(position);
    String _test = _hashMap.get("Text").toString();
查看更多
不美不萌又怎样
3楼-- · 2019-09-20 05:57

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

  });
查看更多
仙女界的扛把子
4楼-- · 2019-09-20 06:03

add below line
listView.setOnItemClickListener(this);

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

查看更多
时光不老,我们不散
5楼-- · 2019-09-20 06:04
 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;
查看更多
Juvenile、少年°
6楼-- · 2019-09-20 06:05

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);
        }
    });
查看更多
够拽才男人
7楼-- · 2019-09-20 06:12

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.

查看更多
登录 后发表回答