列表视图:获取选定行的文本 [英] Listview : Getting the text of the selected row

查看:196
本文介绍了列表视图:获取选定行的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图,每行图像和的TextView
我想对整个行单击文本的价值。
低于code为表现strange.I我得到随机行,而不是一个点击的价值。

我没有得到什么是错的。
任何帮助将是AP preciated。

我这样做,

 公共静态类LazyAdapter延伸BaseAdapter {    私人活动活动;
    私人的ArrayList<&HashMap的LT;字符串,字符串>>数据;
    私有静态LayoutInflater吹气= NULL;
    公共ImageLoader的ImageLoader的;    公共LazyAdapter(活动一,ArrayList的<&HashMap的LT;字符串,字符串> D 1和D){
        活性=一个;
        数据= D;
        吹气=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ImageLoader的=新ImageLoader的(activity.getApplicationContext());
    }    公众诠释的getCount(){
        返回data.size();
    }    公共对象的getItem(INT位置){
        返回的位置;
    }    众长getItemId(INT位置){
        返回的位置;
    }    公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){
        查看VI = convertView;
        如果(convertView == NULL)
            VI = inflater.inflate(R.layout.list_row,NULL);        标题=(TextView中)vi.findViewById(R.id.title); //称号
        ImageView的thumb_image =(ImageView的)vi.findViewById(R.id.list_image); //拇指图像        HashMap的<字符串,字符串>歌=新的HashMap<字符串,字符串>();
        曲= data.get(位置);        //设置列表视图中的所有值
        title.setText(song.get(Meida_listActivity.KEY_TITLE));
        imageLoader.DisplayImage(song.get(Meida_listActivity.KEY_THUMB_URL),thumb_image);
        vi.setOnClickListener(新View.OnClickListener()
        {
            公共无效的onClick(视图v)
            {
                  字符串listValue =(字符串)title.getText();
                  的System.out.println(这是字符串值::: ::::+ listValue);            }
        });
        返回VI;
    }
}


解决方案

您也可以尝试实现点击列表视图,并得到文本都是这样!希望工程..

  listView.setOnItemClickListener(新OnItemClickListener(){
        @覆盖
        公共无效onItemClick(适配器视图<>母公司,观景,
                INT位置,长的id){
            TextView的文本=(TextView的)view.findViewById(R.id.title);
            。字符串lst_txt = text.getText()的toString()修剪();
            的System.out.println(这是串::: ::::值+ lst_txt);
        }
    });

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

解决方案

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

这篇关于列表视图:获取选定行的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆