views:

28

answers:

1

Hi again all,

I'm still trying to implement a search function into my Android app. So far it's going ok, although currently the search can only query one table and display the results for this table (its a ListView using SimpleCursorAdapter).

What I want is to be able to search multiple tables, but I'm not sure how to get this all into one cursor or extend the SimpleCursorAdapter to implement multiple cursors. I see there is a class called CursorJoiner, but I'm not sure what I need to do with it.

Thanks!

I have tried to make a custom cursor[] adapter, but this doesn't return anything and my search results are blank- can anyone help?

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;

just noticed that it's not actually the adapter returning nothing, there's something wrong with my searchactivity...

A: 

What I want is to be able to search multiple tables, but I'm not sure how to get this all into one cursor or extend the SimpleCursorAdapter to implement multiple cursors

If you are using SQLite, implement a JOIN in your SELECT statement. If you have wrapped SQLite in a content provider for some reason, expose another content Uri and support for your multi-table search.

CommonsWare
Erm... how? Sorry, I'm really inexperienced with SQL. I read a little bit about it but it seems like there must be some relationship between the tables, which in my case there isn't- they're both just lists of values. I've tried to make a custom cursor/arrayadapter (see edit above) but that just returns nothing.
Espiandev
@Espiandev: Sorry, "search multiple tables" can have many definitions, and I guessed the wrong one. In your case, if I now understand it correctly, use `MergeCursor` (in SDK) or my `MergeAdapter`, depending on whether or not the rows should look different between the multiple tables you're searching. http://github.com/commonsguy/cwac-merge
CommonsWare
Works fantastically, thank you!
Espiandev