用于listview的Android自定义适配器

时间:2011-07-21 11:24:07

标签: android listview

我有一个自定义列表视图,我正在显示来自JSON输入的图像。 http://i.imgur.com/KILe5.png 当我传递所有项目的黄色星数值时,我有点困惑。我从篮球传到第3项的价值正好是黄色星的1,2,3,意思是第1项应该显示1星,第2项,2星和第3项,3星。

似乎第1项总是得到第3项的值。从最左边的项目读取,是Star1,Star2等,直到Star5。

请参阅以下代码,该代码是我的自定义适配器:

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;

}

}

我有以下XML来描述Star1到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>

编辑:我放入星号的部分来自JSON 它的块没有重复,因为我已经把日志检查:

      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 个答案:

没有答案
相关问题