2013-09-07 2 views
5

목록보기를 채우는 목록 어댑터 클래스가 있습니다. 그러나 내 목록이 어떤 이유로 비어 있으면 그냥 비어 있습니다. 리스트에 "list is empty"라는 항목을 추가하도록하려면 어떻게해야합니까?사용자 지정 목록 어댑터로 listView가 비어 있음을 표시하는 방법

어쩌면 다른 항목, 즉 빈 항목에 대해 다른 layotu 파일을 추가하는 것이었지만 두 가지 유형의보기를 사용하는 방법을 모르십니까? 여기

내 목록 어댑터에 대한 현재 코드 : 활동 레이아웃에서

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(); 
    } 
} 

}

답변

6

ListView에는 setEmptyView라는 특수 기능이 있으므로 어댑터를 변경할 필요가 없습니다. 레이아웃이 비어있을 때 표시 할 메시지로 다른 TextView를 만듭니다.

그런 다음 활동에 :

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

감사합니다. 위대하고 간단한 솔루션입니다. 완벽하게 작동합니다. 빠르고 쉽게 구현할 수 있습니다. – Zapnologica

0

는 또 하나의 텍스트 뷰를 추가 할 수 있습니다. 목록이 비어 있으면 가시성을 설정합니다. 그렇지 않으면 어댑터에서 아무 것도 변경할 필요가 없습니다.

0

이 시도 :

당신은 목록

<TextView 
    android:id="@+id/empty_view" 
    android:layout_width="match_parent" 
    android:layout_height="60px" 
    android:text="@string/empty_text" /> 

mListView.setEmptyView(findViewById(R.id.empty_view)); 

경우 문자열 empty_text 빈입니다 사용자를 나타 내기 위해 빈보기로 텍스트 뷰를 설정할 수 있습니다 "List is Empty"와 같은 값을가집니다.

관련 문제