2

How to show the alert dialog for the empty list view when there are no listview items. Please find the below image having three text fields. I have to implement the empty list view when there are no records/unmatched records in the list.

The list view is implemented as below:

ListView empListView; 
empListView = (ListView)findViewById(R.id.list1 );

I have to show the alert dialog box for the empListView. Please help me with the samplecode/links.

4
  • 1
    What have you tried before, do you have any particular issues? Generally it helps to tell us what problem your facing, that way we can guide you through the issue and you will know how to solve it by yourself next time. Commented Nov 25, 2011 at 5:34
  • 1
    how do you set adapter, show the code.. Commented Nov 25, 2011 at 5:35
  • 1
    CursorAdapter adaptr = new MyCursorAdapter( getApplicationContext(), R.layout.listview, cdata, fields, names); where cdata is a Cursor to return values from query, fields are the list view entities and names is an array to store the listview items. Commented Nov 25, 2011 at 5:37
  • 1
    @avadhani you can check the cdata.getCount() > 0 and peform any action to show the List is empty if getCount is less than 0. Commented Nov 25, 2011 at 5:44

5 Answers 5

2

I think you are paasing arraylist or some other data in setadapter method And if you are using arraylist then you have to check that size of arraylist before calling the setadapter method.

if(a.size()>0)
    {
    lv = (ListView) findViewById(R.id.frendlist);
    lv.setAdapter(new ListAdapter(this, R.id.frendlist, a));
    }
    else
    {
        builder.setMessage(" You Have no friends") 
        .setCancelable(false)   
        .setPositiveButton("Ok", new DialogInterface.OnClickListener() 
        {      
            public void onClick(DialogInterface dialog, int id)
            {

            }   
            }) ; 

        AlertDialog alert = builder.create();
        alert.show();
    }
Sign up to request clarification or add additional context in comments.

Comments

2

As per my opinion, No need to check the size of arraylist or adapter item count.

Instead of displaying alert dialog, you can just display message "Sorry no records found" message on the listview. for the same you have to set the empty view by using setEmptyView() method of ListView.

For example:

listViewFriends.setEmptyView(findViewById(R.id.empty));

2 Comments

@avadhani yes dear, it is the best practice to show empty listview instead of displaying dialog or anything else.
can u help me in searching with special character records of the database queries?
1

It all depends on how you implement the setAdapter method etc. But here is an example:

if(cdata.getCount()==0) {
  //empty, show alertDialog
  AlertDialog.Builder builder = new AlertDialog.Builder(this);
  builder.setMessage("Search is empty")
   .setCancelable(true)
   .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
       }
   });
  AlertDialog alert = builder.create();
} 
else {
  //Not empty, set the adapter or do what you want. 
  empListview.setAdapter(new MyCursorAdapter( getApplicationContext(), R.layout.listview, cdata, fields, names));
}

The code above havent been tested. But should work with minor adjustments, I might have forgotten something.

Comments

1

I think you are paasing arraylist or some other data in setadapter method And if you are using arraylist then you have to check that size of arraylist before calling the setadapter method.

2 Comments

yes, i am passing array list in set adapter method. Please help me with sample code on how to display the alert dialog when the array list is empty.
if(a.size()>0) { lv = (ListView) findViewById(R.id.frendlist); lv.setAdapter(new ListAdapter(this, R.id.frendlist, a)); } else { builder.setMessage(" You Have no friends") .setCancelable(false) .setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { } }) ; AlertDialog alert = builder.create(); alert.show(); }
1
if(cdata.getCount()>0)
{
    CursorAdapter adapter = new MyCursorAdapter( getApplicationContext(), R.layout.listview, cdata, fields, names);
    listview.setAdapter(adapter);
}
else
{
    //create dialog here
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.