开发者

Android custom adapter for listview

I have a custom listview that i'm displaying images from JSON input. http://i.imgur.com/KILe5.png I'm kind of confused when I pass in the value for number of stars in yellow colour for all the items. The value i passed in from basketball to 3rd item in list are exactly 1,2,3 for the yellow stars meaning item 1 should be displaying 1 star, item 2, 2 stars and item 3, 3 stars.

It seems like item 1 is always getting the value of item 3. Reading from left most of an item, is Star1, Star2 and so on until Star5.

Refer to the following code that is my custom adapter:

public class MyAdapter extends ArrayAdapter<ApplicationItem>
{

    int resource;
    String response;
    Context context;
    public ImageLoader imageLoader; 
    private Activity activity;

private static LayoutInflater inflater=null;


//Initialize adapter
public MyAdapter(Context context, int resource, Activity activity, List<ApplicationItem> items) {
    super(context, resource, items);
    this.resource=resource;
    imageLoader=new ImageLoader(activity.getApplicationContext());
  开发者_运维技巧  inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public static class ViewHolder{
    public TextView Publisher;
    public TextView Application;
    public TextView Price;
    public ImageView image;
    public ImageView Star1, Star2, Star3, Star4, Star5;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{


    ApplicationItem App = getItem(position);

    View vi=convertView;
    ViewHolder holder;  

    if(convertView==null)
    {

        vi = inflater.inflate(R.layout.item, null);
        holder=new ViewHolder();
        holder.Publisher=(TextView)vi.findViewById(R.id.Publisher);
        holder.Application=(TextView)vi.findViewById(R.id.Application);
        holder.Price=(TextView)vi.findViewById(R.id.Price);
        holder.image=(ImageView)vi.findViewById(R.id.Icon);
        holder.Star1 = (ImageView)vi.findViewById(R.id.ItemStar1);
        holder.Star2 = (ImageView)vi.findViewById(R.id.ItemStar2);
        holder.Star3 = (ImageView)vi.findViewById(R.id.ItemStar3);
        holder.Star4 = (ImageView)vi.findViewById(R.id.ItemStar4);
        holder.Star5 = (ImageView)vi.findViewById(R.id.ItemStar5);
        vi.setTag(holder);

    }
    else
    {          
        holder=(ViewHolder)vi.getTag();
    }


    holder.Publisher.setText(App.Publisher);
    holder.Application.setText(App.Application);
    holder.Price.setText(App.Price);
    holder.image.setTag(App.ImageURL);
    imageLoader.DisplayImage(App.ImageURL, activity, holder.image);

    switch (Integer.valueOf(App.Rating))
        {
            case 5:
                holder.Star1.setImageResource(R.drawable.star_full);
                holder.Star2.setImageResource(R.drawable.star_full);
                holder.Star3.setImageResource(R.drawable.star_full);
                holder.Star4.setImageResource(R.drawable.star_full);
                holder.Star5.setImageResource(R.drawable.star_full);  
                break;                                      
            case 4:
                holder.Star1.setImageResource(R.drawable.star_full);
                holder.Star2.setImageResource(R.drawable.star_full);
                holder.Star3.setImageResource(R.drawable.star_full);
                holder.Star4.setImageResource(R.drawable.star_full);
                break;
            case 3:
                holder.Star1.setImageResource(R.drawable.star_full);
                holder.Star2.setImageResource(R.drawable.star_full);
                holder.Star3.setImageResource(R.drawable.star_full);
                break;
            case 2:
                holder.Star1.setImageResource(R.drawable.star_full);
                holder.Star2.setImageResource(R.drawable.star_full);
                break;
            case 1:
                holder.Star1.setImageResource(R.drawable.star_full);
                break;
        }

    return vi;

}

}

And I have the following XML to describe the layout of Star1 to Star5

<LinearLayout android:id="@+id/ItemRating" android:orientation="horizontal" android:layout_width="wrap_content" 
android:layout_height="wrap_content" android:layout_alignBaseline="@id/Application" android:layout_alignRight="@id/Publisher" android:layout_below="@id/Price">


<ImageView  android:id="@+id/ItemStar1"   android:src="@drawable/star_empty" 
    android:layout_width="18dip"  android:scaleType="fitXY" android:layout_height="20dip" />   

<ImageView  android:id="@+id/ItemStar2"   android:src="@drawable/star_empty" 
    android:layout_width="18dip"  android:scaleType="fitXY" android:layout_height="20dip" /> 

<ImageView  android:id="@+id/ItemStar3"   android:src="@drawable/star_empty" 
    android:layout_width="18dip"  android:scaleType="fitXY" android:layout_height="20dip" />

<ImageView android:id="@+id/ItemStar4"   android:src="@drawable/star_empty" 
    android:layout_width="18dip"  android:scaleType="fitXY" android:layout_height="20dip" />

<ImageView android:id="@+id/ItemStar5"   android:src="@drawable/star_empty" 
    android:layout_width="18dip"  android:scaleType="fitXY" android:layout_height="20dip" />             

</LinearLayout>

EDIT: the part that I put in number of stars is here from JSON its block is not repeating as i've put a log to check:

      for (int i = 0; i < ja.length(); i++) {
                            JSONObject jo = (JSONObject) ja.get(i);
                            ApplicationItem AI = new ApplicationItem();

                            AI.Rating = jo.getString("Star");

                            m_apps.add(AI);                              
                            m_adapter.notifyDataSetChanged();



                        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜