2010-08-29 26 views
1

我仍然試圖在我的Android應用程序中實現搜索功能。到目前爲止,它還行,儘管目前搜索只能查詢一個表並顯示該表(使用SimpleCursorAdapter的ListView)的結果。搜索需要來自多個遊標/表格的數據

我想要的是能夠搜索多個表,但我不知道如何將這一切全部放入一個遊標或擴展SimpleCursorAdapter以實現多個遊標。我看到有一個名爲CursorJoiner的類,但我不確定我需要做什麼。

謝謝!

我試圖製作一個自定義光標[]適配器,但是這不會返回任何東西,我的搜索結果是空白的 - 任何人都可以幫忙嗎?

public class SearchCursorAdapter extends SimpleCursorAdapter { 

private int currentCursor; 
private int curPosition = 0; 
private int total = 0; 
private Cursor[] curs = null; 
private Context cont; 

public SearchCursorAdapter(Context context, int layout, Cursor c, 
     String[] from, int[] to) { 

    super(context, layout, c, from, to); 
    total = c.getCount(); 

} 

public SearchCursorAdapter(Context context, int layout, Cursor[] c, 
     String[] from, int[] to) { 

    super(context, layout, null, from, to); 
    int l = c.length; 
    for (int i = 0; i < l; i++) { 

     total += c[i].getCount(); 

    } 
    curs = c; 
    currentCursor = 0; 
    cont = context; 
} 

@Override 
public View getView(int position, View view, ViewGroup parent) { 

    if (currentCursor == curs.length) 
     return null; 

    if (curs == null) { 

     //normal shiz 

    } 
    else { 

     Cursor c = curs[currentCursor]; 
     c.moveToPosition(curPosition); 

     if (c.isAfterLast()) { 

      currentCursor++; 
      c = curs[currentCursor]; 
      curPosition = 0; 
      c.moveToPosition(curPosition); 

     } 

     if (view == null) { 
      LayoutInflater vi = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      view = vi.inflate(R.layout.search_row, null); 
     } 

     TextView t1 = (TextView)view.findViewById(R.id.rowitem_text1); 
     TextView t2 = (TextView)view.findViewById(R.id.rowitem_text2); 

     t1.setText(c.getString(1)); 
     t2.setText("Testing"); 

     curPosition++; 

    } 

    return view; 

只注意到它實際上並不是適配器返回什麼,有什麼問題我searchactivity ...

回答

2

我要的是能夠搜索 多個表,但我不知道如何 讓這一切變成一個光標或 延長SimpleCursorAdapter到 實現多個遊標

如果你是使用SQLite,在您的SELECT聲明中實施JOIN。如果出於某種原因在內容提供程序中打包了SQLite,則應公開其他內容Uri並支持您的多表搜索。

+0

呃...怎麼樣?對不起,我對SQL很不熟悉。我讀了一些關於它的內容,但似乎表格之間必定存在某種關係,在我的情況下,它們並不僅僅是價值清單。我試圖製作一個自定義遊標/ arrayadapter(請參閱上面的編輯),但它不會返回任何內容。 – 2010-08-29 14:42:20

+1

@Espiandev:對不起,「搜索多個表」可以有很多定義,我猜錯了。在你的情況下,如果我現在理解正確,使用'MergeCursor'(在SDK中)或我的'MergeAdapter',這取決於在你搜索的多個表之間行是否應該看起來不同。 http://github.com/commonsguy/cwac-merge – CommonsWare 2010-08-29 15:17:49

+0

非常好用,謝謝! – 2010-08-29 15:52:30