Listview : Getting the text of the selected row

佐手、 提交于 2019-12-25 17:16:11

问题


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

回答1:


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



回答2:


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.




回答3:


type data of variable data is ArrayList

    HashMap<String, String> _hashMap = (HashMap<String, String>) data.get(position);
    String _test = _hashMap.get("Text").toString();



回答4:


 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;



回答5:


add below line
listView.setOnItemClickListener(this);

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




回答6:


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

  });


来源:https://stackoverflow.com/questions/18355934/listview-getting-the-text-of-the-selected-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!