Android:在TextViews中而不是ListView中一起显示多个游标数据 [英] Android: Display Multiple Pieces of Cursor Data Together in TextViews instead of a ListView
问题描述
我能够完成拉动不同列索引的随机行,并使用自定义ArrayAdapter(List<String>
)在我的Main Activity中用数据填充ListView
的情况.我想获取相同的数据,并使用它在Main Activity中填充单个textview(或包含两个TextViews
的LinearLayout
). TextView
将采用以下格式:
I am able to accomplish pulling a random row of different column indexes and populate a ListView
in my Main Activity with the data using a custom ArrayAdapter (List<String>
). I want to take that same data and use it to populate a single textview (or LinearLayout
containing two TextViews
) in the Main Activity. That TextView
(s) would be in this format:
String Int:Int 条目
String Int:Int Entry
BookName章节编号:Entry# 条目
BookName Chapter#:Entry# Entry
我有以下代码从SQLite数据库中提取随机信息,并且可以正常工作:
I have the following code to pull my random information from my SQLite Database and it works:
public List<String> getEntry() {
List<String> list = new ArrayList<>();
Cursor cursor = database.rawQuery("SELECT * FROM bookdatabase ORDER BY RANDOM() LIMIT 1", null);
cursor.moveToFirst();
while (cursor != null && cursor.getCount() > 0 & !cursor.isAfterLast()) {
list.add(cursor.getString(3));
list.add(cursor.getString(4));
list.add(cursor.getString(5));
list.add(cursor.getString(6));
cursor.moveToNext();
}
cursor.close();
return list;
}
这是我的自定义适配器,用于显示ListView
中的数据:
Here is my custom adapter that works to show the data in a ListView
:
public EntryAdapter(Context context, int resource1, List<String> entries) {
super(context, resource1, entries);
mContext = context;
mResource1 = resource1;
mEntries = entries;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItem = convertView;
if (listItem == null) {
listItem = LayoutInflater.from(mContext).inflate(mResource1, parent, false);
}
TextView entryTextView = (TextView) listItem.findViewById(R.id.bookName);
entryTextView.setText(mEntries.get(position));
return listItem;
}
在这里,我尝试从主活动中访问它以填充TextView
,而该TextView
当前可以在ListView
中显示它:
Here is where I am trying access it from my main activity to populate a TextView
which currently works to show it in a ListView
:
databaseAccess.open();
mEntry = databaseAccess.getEntry();
databaseAccess.close();
mEntryAdapter = new EntryAdapter(this,
R.layout.entry_item, mEntry);
this.mEntryListView.setAdapter(mEntryAdapter);
先谢谢您.
推荐答案
经过大量研究,我解决了我的问题.我最终使用了一篇文章,该文章包含在整个流程中,而另一篇则在GitHub上;两者都与CursorAdapters有关.最重要的是,我创建了一个游标适配器而不是数组适配器,它完美地启动了.如果有人遇到同样的问题,请在此处发帖:
After much research, I solved my problem. I ended up using the information contained in one post on stack over flow and another on GitHub; both about CursorAdapters. Bottom line, I created a cursor adapter instead of an array adapter and it woks perfectly. The posts are here in case anyone encounters this same problem:
https://github.com/codepath/android_guides/wiki/Populating-a-ListView-with-a-CursorAdapter
这篇关于Android:在TextViews中而不是ListView中一起显示多个游标数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!