我正在爲Android編寫RSS閱讀器。我遇到了一些困難,我無法解決的問題,因爲數據庫不是我的專業知識..所以我想通了,也許你們中的一個可以幫助我!我目前有3個表格(分類,鏈接和提要)。我的目標是將饋送鏈接到多個類別。爲此我使用鏈接表。我的數據庫是一個Android的ContentProvider(源碼),看起來像下面這樣:Android ContentProvider數據庫查詢多個表
| 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);
}
現在我的問題:
我不知道我怎麼會優化這個數據庫佈局,代碼或查詢,所以我會得到更有效的方式我的條目。因爲我相信這個鏈接表或查詢來檢索鏈接是不需要的!或者我以正確的方式做這件事?
感謝,
ANTEK