android - 如何使用自定义列表适配器显示 listView 为空

标签 android listview listadapter

我有一个列表适配器类来填充我的 ListView 。但是,如果由于某种原因我的列表是空的,它就是空白的。我怎样才能让它在列表中添加一个项目说“列表是空的”?

我可能会添加另一个项目,即为空项目制作一个不同的 layotu 文件,但我不确定如何使用两种类型的 View ?

这是我的列表适配器当前的代码:

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) {
        putOrdersIntoArray(ordersObj);
    } else {
        // Orders is null
        Log.e(TAG, " Orders is null, which means the server did not return anything");
    }
}

@Override
public int getCount() {
    return orders.size();
}

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

@Override
public long getItemId(int position) {
    return 0;
}

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

@Override
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(R.id.my_order_item_textView_img);
        holder.txtName = (TextView) convertView.findViewById(R.id.my_order_item_textView_name);
        holder.txtQuantity = (TextView) convertView.findViewById(R.id.my_order_item_textView_quantity);
        holder.txtPrice = (TextView) convertView.findViewById(R.id.my_order_item_textView_price);
        convertView.setTag(holder);
    } 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);
    holder.txtName.setText(orderItem.getName());
    holder.txtQuantity.setText(orderItem.getQuantity());
    holder.txtPrice.setText(orderItem.getPrice());

    convertView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG, "Deleting order in position : " + position);
            deleteOrder(orders.get(position));
            orders.remove(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);
            orders.add(tmpOrder);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

最佳答案

ListView 有一个特殊的函数,叫做setEmptyView,你不需要改变你的适配器。 在您的布局中创建另一个 TextView,其中包含您希望在列表为空时显示的消息。

然后在您的 Activity 中:

View empty = findViewById(R.id.empty);
ListView list=(ListView)findViewById(R.id.list);
list.setEmptyView(empty);

关于android - 如何使用自定义列表适配器显示 listView 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18670277/

相关文章:

c# - 在 .net WinForms ListView 中获取 ColumnHeaders 的高度(在细节模式下)

android - RecyclerView - 在 notifiyItemInserted(0) 之后保持在 0 位置

android - 生成签名的 apk 时魔数(Magic Number)不匹配

java - 如何仅将一个特定的已编译 .class 导入到 java 项目中

android - 在 Android 中构建 TreeView 控件的建议

android - 数据从自定义适配器更改后刷新 ListView

java - 这里的 != 运算符有什么问题?

java - 测试 SimpledateFormat 时的 JUnit AssertionError

Android:不显示 ListFragment 中带有自定义 ListAdapter 的 ListView

android - 如何使用列表适配器在回收器 View 中修改列表?