Android agrega una cadena a la vista de lista si cursor.getCount () devuelve cero
Frecuentes
Visto 993 veces
0
I am performing some queries to a database and I'm showing the results on a listView
.
Esto se hace así:
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);
y MyAdapt
es algo como esto:
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.
¿Alguna sugerencia?
Muchas Gracias
ACTUALIZACIÓN
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?
2 Respuestas
1
You should used the way @aprian told like this: Add the follow segment to your layout, and do nothing in your java code.
<TextView android:id="@+id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="130px"
android:textSize="25px"
android:text="@+string/textview_text"/>
Update, you can also create a layout which contained a ListView named @+id/android:list
y cárgalo usando setContentView
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal">
<ListView android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView android:id="@+id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="130px"
android:textSize="25px"
android:text="@+string/textview_text"/>
</LinearLayout>
Respondido 28 ago 12, 12:08
Thanks but the problem is that I'm using this android.R.id.listandroid.R.id.list
diseño - Favolas
1
add a View with id @android:id/empty
to your layout. it will shown whenever ListView
esta vacio
Respondido 28 ago 12, 10:08
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas android listview cursor or haz tu propia pregunta.
No debería ser
proc.getCount()
en lugar deprocura.getCount()
? - sdabetif bd(db?) is database object you should not close it because cursor need opened db connection ... - Selvin
also you dont need to do all these to add a layout when empty, there is specifically a layout defined when the list is empty check the doc for ListaActividad - nandeesh
@fiddler Sure it is. Lost in translation from my native language to English. Corrected that - Favolas
android.R.id.empty will not be there in your xml. If you are setting it programmatically. Create one more xml and inflate it and you can use setEmptyView - nandeesh