我正在对数据库执行一些查询,并将结果显示在listView
.
这样做是这样的:
bd.open();
ListView listContent = (ListView)findViewById(android.R.id.list);
Cursor proc = bd.getData(3,0,query);
MyAdapt cursorAdapter = new MyAdapt(this, proc,0);
listContent.setAdapter(cursorAdapter);
MyAdapt
是这样的:
public class MyAdapt extends CursorAdapter {
private final LayoutInflater mInflater;
private int n;
public MyAdapt(Context context, Cursor c, int dbColumn) {
super(context, c);
mInflater = LayoutInflater.from(context);
mContext = context;
n = dbColumn;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView fName = (TextView) view.findViewById(android.R.id.text1);
fName.setText(cursor.getString("Something returned by the cursor")));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
if (context.getClass().getName().equals("something")) {
final View view = mInflater.inflate(R.layout.proclist, parent, false);
return view;
} else {
final View view = mInflater.inflate(android.R.layout.simple_list_item_1, parent, false);
return view;
}
}
}
当我执行返回结果的查询时,这可以正常工作并显示我想要的结果。
如果查询没有返回任何内容,则不会在listView
.
在这种情况下(查询不返回任何内容)我想显示文本"Somenthing not found!"
。
这是我修改过的:
bd.open();
ListView listContent = (ListView)findViewById(android.R.id.list);
Cursor proc = bd.getData(3,0,query);
if (proc.getCount()==0) {
LayoutInflater li = LayoutInflater.from(this);
View layout = li.inflate(android.R.layout.simple_list_item_1, null);
TextView fName = (TextView) layout.findViewById(android.R.id.text1);
fName.setText(getString(R.string.notFound));
} else {
MyAdapt cursorAdapter = new MyAdapt(this, proc,0);
listContent.setAdapter(cursorAdapter);
}
这不起作用,我不明白为什么。
有什么建议么?
谢谢
更新
if statement
在删除的所有内容中遵循您的建议并将其放入:
View empty = findViewById(android.R.id.empty);
TextView emptyText = (TextView)empty.findViewById(android.R.id.empty);
emptyText.setText(getString(R.string.notFound));
listContent.setEmptyView(empty);
这是行不通的。它给了我一个NullPointerException
开emptyText.setText(getString(R.string.notFound));
这不应该是这样的吗?