2010-08-29 53 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