gpt4 book ai didi

java - 糖 ORM : How to display values

转载 作者:太空狗 更新时间:2023-10-29 12:40:57 29 4
gpt4 key购买 nike

我通过 Android Studio 使用 Sugar ORM 进行 Android 开发。

但我想我有一个非常相似的问题。如何将一个/多个结果查询显示为字符串或整数?我的实体看起来像这样:

public class PersonsDatabase extends SugarRecord<PersonsSelection>{
String adultText, childText;
int adultCount, childCount;

public PersonsDatabase()
{

}
public PersonsDatabase(String adultText, String childText, int adultCount, int childCount)
{
this.adultText = adultText;
this.childText = childText;

this.adultCount = adultCount;
this.childCount = childCount;

this.save();
}

它保存正确。但是当我想这样显示时:

public class PersonsSelection extends Activity {

ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_persons_selection);

PersonsDatabase personsDatabase = new PersonsDatabase("1 Adult","No Childs",1,0);
List<PersonsDatabase> personsList = PersonsDatabase.findWithQuery(PersonsDatabase.class,"Select adult_Text from PERSONS_DATABASE");

list = (ListView)findViewById(R.id.listView);
list.setAdapter(new ArrayAdapter<PersonsDatabase>(this, android.R.layout.simple_list_item_1, personsList));
}

我得到类似这样的信息:PACKAGENAME.PersonsDatabase@4264c038但我想要我在构造函数中编写的值。

感谢您的帮助。

最佳答案

来自docs on ArrayAdapter :

However the TextView is referenced, it will be filled with the toString() of each object in the array. You can add lists or arrays of custom objects. Override the toString() method of your objects to determine what text will be displayed for the item in the list.

简而言之:只需覆盖 PersonsDatabase 类中的 toString() 方法即可返回所需的文本表示。

或者:

To use something other than TextViews for the array display, for instance, ImageViews, or to have some of data besides toString() results fill the views, override getView(int, View, ViewGroup) to return the type of view you want.

(再次来自文档)。有很多关于如何去做的例子。

关于java - 糖 ORM : How to display values,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26014181/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com