Android:项目位置0

时间:2015-08-28 10:56:25

标签: android gridview

我已经在这个问题上待了一整天了,现在还无法解决。我希望你能提供帮助。我有一个自定义GridView,它在项目位置0上加载图像时出现问题:

  1. 每次都会被调用3次。
  2. 在我采取某些行动之前,图像没有显示(让我说我点击一个按钮导航到另一个页面然后返回到该页面。现在我可以看到图像了。)
  3. 我使用的是CustomGridView:ExpandableHeightGridView

    我的适配器:

    private final class SubCatListAdapter extends BaseAdapter implements AdapterView.OnItemClickListener {
    
        public JSONArray data;
        private final Context context;
    
        public SubCatListAdapter(Context context, JSONArray values) {
            super();
            this.context = context;
            this.data = values;
        }
    
        // ...
    
        class ViewHolder {
    
            public TextView txtViewCatName;
            public SimpleDraweeView imgLogo;
    
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
            View rowView = convertView;
            ViewHolder viewHolder = null;
    
            if (rowView == null) {
    
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                rowView = inflater.inflate(R.layout.gamification_subcat_adapter, parent, false);
                viewHolder = new ViewHolder();
                viewHolder.imgLogo = (SimpleDraweeView) rowView.findViewById(R.id.imgLogo);
                viewHolder.txtViewCatName = (TextView) rowView.findViewById(R.id.txtViewCatName);
    
                rowView.setTag(viewHolder);
    
            } else {
    
                viewHolder = (ViewHolder) rowView.getTag();
            }
    
            // data
            try {
                JSONObject node = getItem(position);
                viewHolder.txtViewCatName.setText(Html.fromHtml(node.getString("CatName")).toString());
                String catLogo = node.getString("CatLogoPath");
                viewHolder.imgLogo.setImageURI(Uri.parse(catLogo));
                Log.d("TAG", "position: " + position);
    
                return rowView;
    
            } catch (Exception e) {
                e.printStackTrace();
                return new View(context);
            }
        }
    
    }
    

    Log.d标记显示位置0被加载3次:

    D/TAG﹕ position: 0
    D/TAG﹕ position: 0
    D/TAG﹕ position: 0
    D/TAG﹕ position: 1
    D/TAG﹕ position: 2
    D/TAG﹕ position: 3
    

    这是位于0,1,2,3位置的项目

    enter image description here

    请帮助~~我快死了...先谢谢了!

1 个答案:

答案 0 :(得分:1)

private final class SubCatListAdapter extends BaseAdapter implements AdapterView.OnItemClickListener {

public JSONArray data;
private final Context context;

public SubCatListAdapter(Context context, JSONArray values) {
    super();
    this.context = context;
    this.data = values;
}

// ...

class ViewHolder {

    public TextView txtViewCatName;
    public SimpleDraweeView imgLogo;

}

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

    View rowView = convertView;
    ViewHolder viewHolder = new ViewHolder();



        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(R.layout.gamification_subcat_adapter, parent, false);

        viewHolder.imgLogo = (SimpleDraweeView) rowView.findViewById(R.id.imgLogo);
        viewHolder.txtViewCatName = (TextView) rowView.findViewById(R.id.txtViewCatName);

        rowView.setTag(viewHolder);



    // data
    try {
        JSONObject node = getItem(position);
        viewHolder.txtViewCatName.setText(Html.fromHtml(node.getString("CatName")).toString());
        String catLogo = node.getString("CatLogoPath");
        viewHolder.imgLogo.setImageURI(Uri.parse(catLogo));
        Log.d("TAG", "position: " + position);

        return rowView;

    } catch (Exception e) {
        e.printStackTrace();
        return new View(context);
    }
}

}