Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于listview的Android自定义适配器_Android_Listview - Fatal编程技术网

用于listview的Android自定义适配器

用于listview的Android自定义适配器,android,listview,Android,Listview,我有一个自定义的listview,显示来自JSON输入的图像。 当我输入所有项目的黄色星星数时,我有点困惑。我从basketball传递给列表中第三个项目的值正好是1,2,3,表示项目1应该显示1颗星、项目2,2颗星和项目3,3颗星 似乎项目1总是得到项目3的值。从一个项目的最左边开始阅读,是Star1、Star2,依此类推直到Star5 请参阅我的自定义适配器的以下代码: public class MyAdapter extends ArrayAdapter<ApplicationIte

我有一个自定义的listview,显示来自JSON输入的图像。 当我输入所有项目的黄色星星数时,我有点困惑。我从basketball传递给列表中第三个项目的值正好是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;

}
公共类MyAdapter扩展了ArrayAdapter
{
智力资源;
字符串响应;
语境;
公共图像加载器;
私人活动;
专用静态充气机=空;
//初始化适配器
公共MyAdapter(上下文上下文、int资源、活动、列表项){
超级(上下文、资源、项目);
这个。资源=资源;
imageLoader=新的imageLoader(activity.getApplicationContext());
充气器=(LayoutInflater)activity.getSystemService(Context.LAYOUT\u充气器\u SERVICE);
}
公共静态类视图持有者{
公共文本视图发布者;
公共文本视图应用程序;
公共文本视图价格;
公众形象;
公共图像视图Star1、Star2、Star3、Star4、Star5;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图)
{
ApplicationItem App=getItem(位置);
视图vi=转换视图;
视窗座;
if(convertView==null)
{
vi=充气机充气(R.layout.item,空);
holder=新的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=(ViewHolder)vi.getTag();
}
holder.Publisher.setText(App.Publisher);
holder.Application.setText(App.Application);
holder.Price.setText(应用程序价格);
holder.image.setTag(App.ImageURL);
imageLoader.DisplayImage(App.ImageURL、活动、holder.image);
开关(整数值(应用额定值))
{
案例5:
holder.Star1.setImageResource(R.drawable.star_full);
holder.Star2.setImageResource(R.drawable.star_full);
holder.Star3.setImageResource(R.drawable.star_full);
持有者.Star4.setImageResource(R.drawable.star_full);
持有者.Star5.setImageResource(R.drawable.star_full);
打破
案例4:
holder.Star1.setImageResource(R.drawable.star_full);
holder.Star2.setImageResource(R.drawable.star_full);
holder.Star3.setImageResource(R.drawable.star_full);
持有者.Star4.setImageResource(R.drawable.star_full);
打破
案例3:
holder.Star1.setImageResource(R.drawable.star_full);
holder.Star2.setImageResource(R.drawable.star_full);
holder.Star3.setImageResource(R.drawable.star_full);
打破
案例2:
holder.Star1.setImageResource(R.drawable.star_full);
holder.Star2.setImageResource(R.drawable.star_full);
打破
案例1:
holder.Star1.setImageResource(R.drawable.star_full);
打破
}
返回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();



                        }
for(int i=0;i
你能显示你通过星星数的部分吗?法拉第-我已经编辑了我的帖子,包括我从JSON数组获得评级的部分