我需要你的帮助。我有一个listview和搜索edittext,当我键入搜索记录并从db匹配时,它显示所有记录。但是,如果没有匹配的记录,则只显示任何内容。我想做的是显示消息找不到记录
这是我到目前为止所尝试的:
etSearch.addTextChangedListener(new TextWatcher (){
public void afterTextChanged(Editable arg0) {
}
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
}
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
adapter.getFilter().filter(cs.toString());
}
});
adapter.setFilterQueryProvider(new FilterQueryProvider() {
@Override
public Cursor runQuery(CharSequence constraint) {
String searchBy = constraint.toString();
if(cursor.getCount() == 0) {
Log.e("Cursor", "" + "Empty result");
}
return dbHelper.searchProduct(searchBy, telco);
}
});
更新
public Cursor searchProduct(String product, String telco)
{
final String MY_QUERY
= "SELECT * " +
"FROM products " +
"WHERE telco_code = '" + telco + "' " +
"AND product_code LIKE '%" + product + "%' " + // LIKE '%
"AND product_description LIKE '%" + product + "%';";
Cursor localCursor =
this.myDataBase.rawQuery(MY_QUERY, null);
if (localCursor != null)
localCursor.moveToFirst();
return localCursor;
}
有什么想法吗?我很乐意感谢你的帮助。感谢。
答案 0 :(得分:0)
替换
if(cursor == null)
与
if(cursor != null && cursor.getCount() == 0)
修改强>
以这种方式实施runQuery()
方法:
@Override
public Cursor runQuery(CharSequence constraint) {
String searchBy = constraint.toString();
Cursor cursor = dbHelper.searchProduct(searchBy, telco);
if(cursor != null && cursor.getCount() == 0){
new AlertDialog.Builder(this)
.setTitle("No Records Found")
.setMessage("No records are available.")
.setPositiveButton(android.R.string.ok, null)
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
return cursor;
}
试试这个。这应该有用。
答案 1 :(得分:0)
更改以下行
if(cursor == null)
到
if(cursor != null &&cursor.getCount() == 0)
希望这会对你有所帮助。
答案 2 :(得分:0)
使用ListView的空视图功能。
在放置ListView的布局中,还添加了一个在ListView为空时显示的视图。然后在初始化(在代码中)ListView时,分配空视图。
ListView现在将显示和隐藏空视图,具体取决于ListView是否已加载数据。
activity_main.xml中:
<FrameLayout ... >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/emptyView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Nothing to show..." />
</Frameayout>
MainActivity.java:
ListView listView = (ListView) findViewById(R.id.list);
listView.setEmptyView(findViewById(R.id.emptyView));
listView.setAdapter(adapter);
答案 3 :(得分:0)
我用这个解决了这个问题:
adapter.setFilterQueryProvider(new FilterQueryProvider() {
@Override
public Cursor runQuery(CharSequence constraint) {
String searchBy = constraint.toString();
Cursor newCursor = dbHelper.searchProduct(searchBy, telco);
if(newCursor.getCount() == 0) {
Log.e("Cursor", "Empty" );
runOnUiThread(new Runnable() {
@Override
public void run() {
tvHeader.setVisibility(View.VISIBLE);
tvHeader.setText(getResources().getString(R.string.no_products));
}
});
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
tvHeader.setVisibility(View.GONE);
}
});
}
return newCursor;
}
});