我很困惑如何將兩個單獨的數據庫查詢合併到一個listView中。如何將來自兩個數據庫表的數據合併到一個列表中查看
目前,我的ListView通過以下適配器,詢問我的數據庫中損壞的部件表,並提供損壞的組件列表一定位置填入:
private class MyListAdapter extends ResourceCursorAdapter {
// In your ListActivity class, create a new inner class that extends ResourceCursorAdapter.
//This inner class is the custom CursorAdapter we will use to manage how data is bound to a list item:
public MyListAdapter(Context context, Cursor cursor) {
super(context, R.layout.row_location, cursor);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView text_first_line = (TextView) view.findViewById(R.id.location_row_item_main_text);
TextView text_second_line = (TextView) view.findViewById(R.id.location_row_item_secondary_text);
ImageView flagIcon = (ImageView) view.findViewById(R.id.flagIcon);
String row_text_component = cursor.getString(cursor.getColumnIndex(RMDbAdapter.COMPONENT));
String row_text_position = ", Position " + cursor.getString(cursor.getColumnIndex(RMDbAdapter.POSITION));
if(row_text_position.equals(", Position Not Applicable")){
row_text_position = "";
}
String row_text_action = " - " + cursor.getString(cursor.getColumnIndex(RMDbAdapter.ACTION_REQUIRED));
text_first_line.setText(row_text_component + row_text_position + row_text_action);
text_second_line.setText("Dexion Speedlock, S Duty, 3000mm");
String risk = cursor.getString(cursor.getColumnIndex(RMDbAdapter.RISK));
if (risk.equals("Red Risk")){
flagIcon.setImageResource(R.drawable.red_flag);
}
else if (risk.equals("Green Risk")){
flagIcon.setImageResource(R.drawable.green_flag);
}
else if (risk.equals("No Risk")){
flagIcon.setImageResource(R.drawable.note);
}
}
}
當我打電話這是觸發以下當活動開始:
private void setAdapter(){
// Get a Cursor for the list items
Cursor listComponentCursor = rmDbHelper.fetchDamagedComponentsForLocation(locationId);
componentCursorSize = listComponentCursor.getCount();
startManagingCursor(listComponentCursor);
// set the custom list adapter
setListAdapter(new MyListAdapter(this, listComponentCursor));
}
所以我也想創建一個單獨的表第二查詢(這個時候問題表),這已損壞的組件列表下添加到ListView。
讀這個Listview from multiple tables?,我相信我應該使用加入或合併遊標。但是,從我研究的內容來看,我仍然不知道如何將任何概念集成到我的代碼中。
任何人都可以指出我正確的方向嗎?
嗨toadzky,感謝您的快速響應和提示(將整合這些我們我得到一個機會)。你看到的是所有填充listView的代碼 - 我沒有覆蓋newView(以前沒有聽說過)。你能詳細說明這個元素嗎?此外,我在每個表中都有不同的列名,您是否有過如何覆蓋getItemViewType的示例?感謝您的幫助。 – Scamparelli
有2個方法需要用CursorAdapter重寫:newView和bindView。 newView膨脹或創建視圖。 bindView將值分配給視圖。它看起來像ResourceCursorAdapter子類爲你照顧。你有任何具體的理由來使用該子類?如果沒有,只需擴展CursorAdapter – toadzky
那麼,如果代碼正在工作,那麼我是否需要newView? – Scamparelli