1

i have an ArrayList and one BaseAdapter to inflate my layout, when one array from arraylist is empty, the value on layout show text "NaN%" it is so annoying, i want handle an empty array from my arraylist with setText as (0%). so how to handle empty value from ArrayList in baseadapter android? this is my code :

   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

i want to handle if listData.get(position).getpersen_hke1() is empty value, it gonna be holder.persen_hke1.setText("0%") how to solve it? thanks before

3
  • In your getView if(listData == null ){ return null; } Commented Jun 12, 2013 at 4:17
  • it still show text as NaN... Commented Jun 12, 2013 at 4:21
  • when you do "setText" on the textview you can test the content of the data you are using ex : if(listData.get(position).isGood){}else{}
    – An-droid
    Commented Jun 12, 2013 at 9:29

1 Answer 1

0

Change your getView() like this

@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;
}
7
  • Check my answer it will work properly..look at the commented lines Commented Jun 12, 2013 at 4:31
  • i got an error... no suggestion available on line nabDetail.getpersen_hke1() != null Commented Jun 12, 2013 at 4:42
  • first it return to double, when adapter get value it will change to string with code : holder.persen_hke1.setText(String.format("%.5f",listData.get(position).getpersen_hke1())+"%"); Commented Jun 12, 2013 at 4:51
  • if it is String check like this nabDetail.getpersen_hke1().equals("") look at my code it will be solve your problem.. Commented Jun 12, 2013 at 5:01
  • i mean first it will return to double.... and when get value it will change to string.. its still doesn't work Commented Jun 12, 2013 at 6:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.