I am performing some queries to a database and I'm showing the results on a listView
.
This is done like this:
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);
and MyAdapt
is something like this:
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;
}
}
}
When I perform a query that returns results, this is working ok and showing the results that I want.
If the query returns nothing, nothing Is shown on the listView
.
In this case (where query returns nothing) I want to display the text "Somenthing not found!"
.
This is what I've modified:
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);
}
This is not working and I don't understand why.
Any suggestions?
Thanks
UPDATE
Following yours suggestions inside the if statement
removed everything and put this:
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);
This is not working. It's giving me a NullPointerException
on emptyText.setText(getString(R.string.notFound));
Wasn't this supposed to be like this?
proc.getCount()
instead ofprocura.getCount()
?