データベースに対していくつかのクエリを実行しており、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));
こんなはずじゃなかったの?