2013-06-12 58 views
1

我有一個ArrayList和一個BaseAdapter來擴充我的佈局,當arraylist中的一個數組爲空時,佈局上的值顯示文本「NaN%」它非常煩人,我想要用setText作爲(0%)從我的數組列表中處理一個空數組。所以如何在baseadapter android中處理ArrayList中的空值?這是我的代碼:如何在ArrayList中處理來自ArrayList的空值Android

public class TabelNABDetail extends BaseAdapter { 
    Context context; 
    ArrayList<NabDetail> listData;//stack 
    int count; 

    public TabelNABDetail(Context context, ArrayList<NabDetail>listData){ 
     this.context=context; 
     this.listData=listData; 
     this.count=listData.size(); 
    } 
    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
//  return count; 
     return (listData==null) ? 0 : listData.size(); 
    } 

    @Override 
    public Object getItem(int arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    @Override 
    public View getView(int position, View v, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     View view= v; 
     ViewHolder holder= null; 


     if(view==null){ 
      LayoutInflater inflater=(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE); 
      view= inflater.inflate(R.layout.layout_tabel_detailnab, null); 
      holder=new ViewHolder(); 
      holder.persen_hke1=(TextView)view.findViewById(R.id.varpersenke1); 
      view.setTag(holder); 
     } 
     else{ 
      holder=(ViewHolder)view.getTag(); 

     } 

     if (position==0) { 
      holder.persen_hke1.setText("0%"); 


    } else { 
     holder.persen_hke1.setText(String.format("%.5f",listData.get(position).getpersen_hke1())+"%"); 

    } 
     return view; 
    } 

    static class ViewHolder{ 
     TextView ,persen_hke1; 

    } 

enter image description here

我想處理,如果listData.get(位置).getpersen_hke1()爲空值,它會是holder.persen_hke1.setText("0%")如何解決呢?由於之前

+0

更改getView()在您的getView如果(的ListData == NULL){ \t \t \t \t \t返回NULL; \t \t} – Raghunandan

+0

它仍然顯示文本爲NaN ... –

+0

當您在textview上執行「setText」時,您可以測試使用的數據的內容例如:if(listData.get(position).isGood){}其他{} –

回答

0

這樣

@Override 
public View getView(int position, View v, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    View view= v; 
    ViewHolder holder= null; 

    if(view==null){ 
     LayoutInflater inflater=(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE); 
     view= inflater.inflate(R.layout.layout_tabel_detailnab, null); 
     holder=new ViewHolder(); 
     holder.persen_hke1=(TextView)view.findViewById(R.id.varpersenke1); 
     view.setTag(holder); 
    } else { 
     holder=(ViewHolder)view.getTag(); 
    } 

    if (position==0) { 
     holder.persen_hke1.setText("0%"); 
    } else { 
     **/////// these lines are extra lines add into your code** 

     NabDetail nabDetail = listData.get(position) 
     if(!nabDetail.getpersen_hke1().equals("")) 
      holder.persen_hke1.setText(String.format("%.5f",listData.get(position).getpersen_hke1())+"%"); 
     } else { 
      holder.persen_hke1.setText("0%"); 
     } 
    } 
    return view; 
} 
+0

請看看我上面的編輯..謝謝 –

+0

檢查我的答案,它會正常工作..看看註釋行 –

+0

我得到了一個錯誤...沒有建議行nabDetail.getpersen_hke1()!= null –