我正在为 Android 编写一个 RSS 阅读器。我遇到了一些我无法解决的问题,因为数据库不是我的专长。所以我想也许你们中的一个人可以帮助我!我目前有 3 个表格(类别、链接和提要)。我的目标是将提要链接到多个类别。因此,我使用的是链接表。我的数据库是一个 Android ContentProvider (sqlite),如下所示:
| Categories | | Links | | Feeds |
|------------| |---------| |-------|
| _ID | | Category| | _ID |
| Title | | Feed | | Title |
| URL |
我目前在我的 FeedListActivity 中编写了以下代码来检索链接列表及其提要。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//gets the intent URI to retrieve the Feeds.
Intent intent = getIntent();
if (intent.getData() == null) {
intent.setData(Feeds.CONTENT_URI);
}
// stores the Category id so it knows from what category it came from
mCatId = intent.getIntExtra("catId", -1);
getListView().setOnCreateContextMenuListener(this);
// gets all Links that have the category placed
Cursor links = managedQuery(
Links.CONTENT_URI,
NewsReadUtils.LINK_PROJECTION_STRING,
Links.CATEGORY+ " = "+ mCatId, null, null);
String query = "";
Cursor cursor = null;
if(links.getCount()>0){
// if there are links available try to get them and build a query.
links.moveToFirst();
do{
if (links.isFirst()) {
query = Feeds._ID+ " = "+links.getString(links.getColumnIndex(Links.FEED));
}else{
query += " OR " +Feeds._ID+ " = "+links.getString(links.getColumnIndex(Links.FEED));
}
}while(links.moveToNext());
cursor = managedQuery(getIntent().getData(), PROJECTION ,query, null,
Feeds.DEFAULT_SORT_ORDER);
}
Cursor cursor = managedQuery(getIntent().getData(), PROJECTION ,Feeds._ID+ " = "+ mCatId, null,
Feeds.DEFAULT_SORT_ORDER);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor,
new String[] { Feeds.TITLE }, new int[] { android.R.id.text1 });
setListAdapter(adapter);
}
现在我的问题:
我想知道如何优化这个数据库布局、代码或查询,以便以更有效的方式获取我的条目。因为我相信不需要此链接表或检索链接的查询!还是我这样做是正确的方式?
谢谢,
安泰克