编程语言
首页 > 编程语言> > Java-Sugar ORM:如何显示值

Java-Sugar ORM:如何显示值

作者:互联网

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

但是我想我有一个非常类似的问题.
如何将一个/多个结果查询显示为String或int?
我的实体看起来像这样:

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.

(再次来自文档).关于如何执行此操作的大量示例.

标签:sugarorm,android-studio,java,android
来源: https://codeday.me/bug/20191121/2050192.html