2013-06-12 57 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 –