2012-10-24 27 views
1

我很困惑如何將兩個單獨的數據庫查詢合併到一個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?,我相信我應該使用加入合併遊標。但是,從我研究的內容來看,我仍然不知道如何將任何概念集成到我的代碼中。

任何人都可以指出我正確的方向嗎?

回答

2

雖然你不顯示它,但我會假設你覆蓋了遊標適配器的newView方法。 MergeCursor只會將一個查詢的結果堆疊在下一個上面。 CursorJoiner幾乎將結果並排放置。這聽起來像你想要一個MergeCursor。

如果您只想將第二個查詢的結果連接到第一個查詢,請執行兩個查詢並創建一個MergeCursor。如果您需要它們具有不同的佈局,或者它們具有不同的列名稱,則需要覆蓋適配器中的getItemViewType方法。然後,在您的newView方法中,您可以根據類型誇大不同的視圖。在bindView中,您需要檢查類型並將正確的值應用於正確的視圖。

一對夫婦爲你的代碼的隨機提示:

1)使用ViewHolder模式,它的方式更快。

2)它總是好做"literal string".equals(variable)而不是variable.equals("literal string")因爲字面字符串不能拋出NullPointerException

+0

嗨toadzky,感謝您的快速響應和提示(將整合這些我們我得到一個機會)。你看到的是所有填充listView的代碼 - 我沒有覆蓋newView(以前沒有聽說過)。你能詳細說明這個元素嗎?此外,我在每個表中都有不同的列名,您是否有過如何覆蓋getItemViewType的示例?感謝您的幫助。 – Scamparelli

+0

有2個方法需要用CursorAdapter重寫:newView和bindView。 newView膨脹或創建視圖。 bindView將值分配給視圖。它看起來像ResourceCursorAdapter子類爲你照顧。你有任何具體的理由來使用該子類?如果沒有,只需擴展CursorAdapter – toadzky

+0

那麼,如果代碼正在工作,那麼我是否需要newView? – Scamparelli

相關問題