返回对象从CursorAdapter.get() [英] Return object from CursorAdapter.get()

查看:188
本文介绍了返回对象从CursorAdapter.get()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我压倒一切的CursorAdapter,我需要得到最后一个项目,问题是,CursorAdapter的具有实际上是get()方法...但来源是一个数据库,它返回一个普通的对象! (我甚至不知道它是什么,我希望它返回一个游标对象,而不是...)

Neverthless,我怎么可以让它回到我的包装分贝排类的实例?

例: 说我的分贝有行这样的:

ID |名字|姓

我从做一个类的

现在我想有一个人获得(int i)以从游标适配器方法...

解决方案
  

现在我想有一个人获得(int i)以从游标适配器方法...

这似乎是一个奇怪的要求。我会通过游标本身(或光标从CursorAdapter的的的getItem()返回)在我的活动一个普通的方法来代替。 但是的下面是基本步骤来创建一个人员获得()方法。

创建Person类:

 公共类Person {
    长ID;
    字符串的firstName;
    字符串姓氏;
}
 

和您的自定义的CursorAdapter只需使用的方法是这样的:

 公众人物得到(INT位置){
    光标光标= getCursor();
    人的人;
    如果(cursor.moveToPosition(位置)){
        人=新的Person();
        person.id = cursor.getLong(cursor.getColumnIndex(_ ID));
        person.firstName = cursor.getString(cursor.getColumnIndex(名字));
        person.surname = cursor.getString(cursor.getColumnIndex(姓));
        results.add(人);
    }

    返回的人;
}
 

I'm overriding CursorAdapter and I need to get the last item, problem is that CursorAdapter has actually a get() method...but source is a db and it returns a plain object!! (I don't even know what is it, I'd expect it returning a Cursor object instead...)

Neverthless, how can I make it return an instance of my Wrapper db row class?

Example: say my db has rows like these:

id|first name| surname

I'd make a class Person from that.

Now I'd like to have a Person get(int i) method from cursor adapter...

解决方案

Now I'd like to have a Person get(int i) method from cursor adapter...

This seems like a strange request. I would pass the Cursor itself (or the Cursor returned from CursorAdapter's getItem()) to a regular method in my Activity instead. But here are the basic steps to create a Person get() method.

Create your Person class:

public class Person {
    long id;
    String firstName;
    String surname;
}

And in your custom CursorAdapter simply use a method like this:

public Person get(int position) {
    Cursor cursor = getCursor();
    Person person;
    if(cursor.moveToPosition(position)) {
        person = new Person();
        person.id = cursor.getLong(cursor.getColumnIndex("_id"));
        person.firstName = cursor.getString(cursor.getColumnIndex("firstName"));
        person.surname = cursor.getString(cursor.getColumnIndex("surname"));
        results.add(person);
    }

    return person;
}

这篇关于返回对象从CursorAdapter.get()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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