Iterate loop inside getView() method of ArrayAdapter<CustomWrapperClassObject>

1.3k Views Asked by At

I have a data wrapper class where i set & get some values, adding to this i have some dynamic values coming from server and i have added a SortedMap in this wrapper class to set when i receive it from server without knowing what data is that, and then i am adding this CustomWrapperClassObject into a ArrayList, got to display ArrayList data inside a listview.

Adapter extends ArrayAdapter

Now when i try to iterate ArrayList.get(position), it returns a customObject. All other values set fine but when i get a SortedMap and try to iterate through its keys. SortedMap values are mixed up in list rows. Like view 1 in listview contains values for the second view as well and as I scroll down and up values are being added in all the views.

Appreciate any help.

class CustomWrapperClassObject{
 public String a;
 public String b;
 SortedMap<String,String> dynamicValues = new TreeMap<String,String>();

 public String getA(){
 }
 public void setA(String a){
 }
 ====same way for b=====
 public SortedMap<String,String> getDynamicValues(){
 }
 public void setDynamicValues(SortedMap<String,String> values){
 }
}

getView method of ArrayAdapter<> =========================================================

public View getView(final int position, View convertView, ViewGroup parent) 
        {
            View v = convertView;
            ViewHolder holder = null;

            if (v == null) 
            {
                holder=new ViewHolder();  
                v = vi.inflate(R.layout.row_item, null); 

                holder.a=(TextView) v.findViewById(R.id.tv_a);
                holder.b=(TextView)v.findViewById(R.id.tv_b);
                holder.dynamicValue=(TextView)v.findViewById(R.id.tv_dynamic);


                v.setTag(holder);
            }
            else
                holder=(ViewHolder)v.getTag();

            CustomWrapperClassObject customWrapperClassObject= ArrayList.get(position);


            holder.a.setText(customWrapperClassObject.getA());
            holder.b.setText(customWrapperClassObject.getB());


            if(customWrapperClassObject.getDynamicValues()!= null && customWrapperClassObject.getDynamicValues().size() > 0)
            {
                SortedMap<String,String> mDataObject =  customWrapperClassObject.getDynamicValues();
                Set<String> keySet = mDataObject.keySet();
                String[] keys = keySet.toArray(new String[keySet.size()]);

                for(int i = 0 ; i < keys.length ; i++)
                {
                    holder.dynamicValue.append(keys[i]+":"+mDataObject.get(keys[i]+"\n");


                }
            }

            return v; 
        }
    }
0

There are 0 best solutions below