gpt4 book ai didi

java - 如何在baseadapter android中处理ArrayList中的空值

转载 作者:行者123 更新时间:2023-12-01 04:37:56 26 4
gpt4 key购买 nike

我有一个 ArrayList 和一个 BaseAdapter 来膨胀我的布局,当 arraylist 中的一个数组为空时,布局上的值显示文本“NaN%”,这太烦人了,我想用 setText 处理 arraylist 中的一个空数组(0%)。那么如何在baseadapter android中处理ArrayList中的空值呢?这是我的代码:

   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(position).getpersen_hke1()为空值,它会是holder.persen_hke1.setText("0%")如何解决?先谢谢了

最佳答案

像这样改变你的 getView()

@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;
}

关于java - 如何在baseadapter android中处理ArrayList中的空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17057552/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com