搜索需要来自多个游标/表的数据
我仍在尝试在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用 SQLite,请在
SELECT
语句中实现JOIN
。如果您出于某种原因将 SQLite 包装在内容提供程序中,请公开另一个内容Uri
并支持多表搜索。If you are using SQLite, implement a
JOIN
in yourSELECT
statement. If you have wrapped SQLite in a content provider for some reason, expose another contentUri
and support for your multi-table search.