0

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?

7
  • 1
    Shouldn't it be proc.getCount() instead of procura.getCount() ?
    – sdabet
    Aug 28, 2012 at 9:28
  • if bd(db?) is database object you should not close it because cursor need opened db connection ...
    – Selvin
    Aug 28, 2012 at 9:32
  • 1
    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 ListActivity
    – nandeesh
    Aug 28, 2012 at 9:32
  • @fiddler Sure it is. Lost in translation from my native language to English. Corrected that
    – Favolas
    Aug 28, 2012 at 9:34
  • 1
    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
    Aug 28, 2012 at 12:08

2 Answers 2

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 and load it using 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>
1
  • Thanks but the problem is that I'm using this android.R.id.listandroid.R.id.list layout
    – Favolas
    Aug 28, 2012 at 11:27
1

add a View with id @android:id/empty to your layout. it will shown whenever ListView is empty

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.