获取ListView项的名称 [英] Get name of ListView item

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

问题描述

我无法通过点击获取的ListView 项目的名称。我填补数据库中的数据与 SimpleCursorAdapter ,当我点击项目我想获得项目名称,但我recive这样的数据

android.content.ContentResolver$CursorWrapperInner@4054b988

我怎样才能从它那里得到的文本?

有是m点击监听器:

保护无效onListItemClick(ListView中升,视图V,INT位置,长的id){
    super.onListItemClick(L,V,位置ID);
    Object对象= this.getListAdapter()的getItem(位置)。
    字符串项= object.toString();
    Log.i(TAG,姓名:+项目);
}


解决方案

其实你得到了你叫什么。每个对象都有一个默认的的toString()方法,它会返回一个字符串在内存中描述它的类名称和位置,这就是你到了那里的结果。你必须重写这个方法有一个有意义的返回值。例如,通过铸造对象来自己的一个有意义的对象

 类项目{
    私人字符串名称;
    公共无效setname可以(字符串名称){
        this.name =名称;
    }
    公共字符串的getName(){
        返回名称;
    }
    公共字符串的toString(){
        返回名称;
    }
}
保护无效onListItemClick(ListView中升,视图V,INT位置,长的id){
    super.onListItemClick(L,V,位置ID);
    Object对象= this.getListAdapter()的getItem(位置)。
    项目项目=(项目)对象;
    字符串名称= item.getName()\\\\或= item.toString();一样的
    Log.i(TAG,姓名:+姓名);
}

I can't get name of ListView item by click. I fill data from database with SimpleCursorAdapter, and when I click on item I want to get item name, but I recive data like this

android.content.ContentResolver$CursorWrapperInner@4054b988.

How I can get text from it?

There is m click listener:


protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Object object = this.getListAdapter().getItem(position);
    String item = object.toString();
    Log.i( TAG, "Name: " + item );
}

解决方案

Actually you got what you've called. Every object has a default toString() method which will return a String describe its class name and position in memory and that's the result you got there. You have to override this method to have a meaningful return value. For example, by casting Object to a meaningful object of your own

class Item{
    private String name;
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
    public String toString(){
        return name; 
    }
}
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Object object = this.getListAdapter().getItem(position);
    Item item = (Item) object;
    String name = item.getName() \\ or = item.toString(); it's the same
    Log.i( TAG, "Name: " + name );
}

这篇关于获取ListView项的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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