标签:sugarorm android-studio java android
我通过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
但是我想要我在构造函数中编写的值.
感谢帮助.
解决方法:
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
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。