搜索需要来自多个游标/表的数据

发布于 2024-09-16 03:33:05 字数 1976 浏览 7 评论 0原文

我仍在尝试在我的 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;

只是注意到它实际上并不是适配器什么也没返回,我的搜索活动有问题......

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗梦空间 2024-09-23 03:33:05

我想要的是能够搜索
多个表,但我不知道如何
将这一切都放入一个光标中或
将 SimpleCursorAdapter 扩展为
实现多个游标

如果您使用 SQLite,请在 SELECT 语句中实现 JOIN。如果您出于某种原因将 SQLite 包装在内容提供程序中,请公开另一个内容 Uri 并支持多表搜索。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文