Zapnologica Zapnologica - 1 year ago 51
Android Question

How to show that a listView is empty with a custom list adapter

I have a list adapter class that fills me list view. But if my list is empty for some reason it is just blank. How can I make it add an item to the list saying "list is empty" ?

I was thing of maybe adding another item, ie making a different layotu file for the empty item, But i am not sure how to use two types of views?

Here is the code currently for my list adapter:

public class MyOrdersAdapter extends BaseAdapter {

private GlobalObjects global;
private Context mContext;
private String TAG = "MyOrdersAdapter";

private boolean listIsEmpty = true;

// Keep all Objects in array
public ArrayList<MyOrdersItem> orders;

* Constructor for creating a menu category
* @param c
* the context of the class calling this class
* @param _global
* the reference to the global object
public MyOrdersAdapter(Context c, GlobalObjects _global) {
mContext = c;
global = _global;
orders = new ArrayList<MyOrdersItem>();
listIsEmpty = true;

JSONArray ordersObj = getCategoriesFromServer();
if (ordersObj != null) {
} else {
// Orders is null
Log.e(TAG, " Orders is null, which means the server did not return anything");

public int getCount() {
return orders.size();

public Object getItem(int position) {
return orders.get(position);

public long getItemId(int position) {
return 0;

public ArrayList<MyOrdersItem> getOrders() {
return orders;

public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;

LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.my_order_item, null);
holder = new ViewHolder();
holder.imageView = (ImageView) convertView.findViewById(;
holder.txtName = (TextView) convertView.findViewById(;
holder.txtQuantity = (TextView) convertView.findViewById(;
holder.txtPrice = (TextView) convertView.findViewById(;
} else {
holder = (ViewHolder) convertView.getTag();
// Temp variable to store the current menu category item
final MyOrdersItem orderItem = (MyOrdersItem) getItem(position);

loadImgFromServer(orderItem.getProductId(), holder.imageView);

convertView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d(TAG, "Deleting order in position : " + position);
return convertView;

* Loads the related image from the server into the ImageView for the specific item
private void loadImgFromServer(String id, ImageView img) {
// Log.i(TAG, "Loading Image with product id: " + id);
int width = 80;
int height = 80;
String url = global.getBaseUrl() + "client/product/image/" + id + "/" + width + "/" + height;
UrlImageViewHelper.setUrlDrawable(img, url, R.drawable.loading_icon, global.getImageCacheTime());
Log.i(TAG, " IMAGE URL: " + url);

* Private View holder class Keeps a reference to the View for the certain components.
* Is used to increase performance of the listView
private class ViewHolder {
ImageView imageView;
TextView txtName;
TextView txtQuantity;
TextView txtPrice;

* Parses the JSON object and builds an array of MenuCategories
* @return
private void putOrdersIntoArray(JSONArray categoryList) {
try {
for (int i = 0; i < categoryList.length(); i++) {

JSONObject tmpObj = categoryList.getJSONObject(i);
String id = tmpObj.getString("productId");
String name = tmpObj.getString("productName");
String quantity = tmpObj.getString("quantity");
String price = tmpObj.getString("productPrice");

MyOrdersItem tmpOrder = new MyOrdersItem(id, name, quantity, price);
} catch (JSONException e) {


Answer Source

ListView has a special function for this called setEmptyView, You don't need to change your adapter. Create another TextView in your layout with the message you want to show when the list is empty.

Then in your Activity:

View empty = findViewById(;
ListView list=(ListView)findViewById(;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download