我正在尝试创建一个图标的Gridview,并将Imageview设置为两个图像之一,如下面的示例所示:



我正在从sqlite内容提供程序中检索两个值,一个用于图章数量,另一个用于最大图章数量。

我使用带有视图持有者模式的自定义适配器,因此尝试在Fragment中设置模型onLoadFinished()。我目前尝试的模型是一个int [],其值中的0或1可以确定是使用绿色印章还是使用灰色印章。但是,这无法正常工作,因为GridView中未显示任何内容。

这是片段中onLoadFinished()的代码片段:

imageSourceModel = new int[data.getInt(data.getColumnIndex(RewardsEntry.COLUMN_REW_MAX_POINTS))];
        int i = data.getInt(data.getColumnIndex(RewardsEntry.COLUMN_REW_POINTS));
        for (int j = 0; j < imageSourceModel.length; j++, i--)   {
            if (i > 0)  {
                imageSourceModel[j] = 1;
            } else  {
                imageSourceModel[j] = 0;
            }
        }
        stampGridAdapter.setModel(imageSourceModel);


以及来自定制适配器的代码:

@Override
public View getView(int i, View view, ViewGroup parent) {
    ViewHolder viewHolder;
    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.list_item_discover, parent, false);
        viewHolder = new ViewHolder(view);

        view.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) view.getTag();
    }
    if (model[i] > 0)   {
        viewHolder.stampView.setImageResource(R.drawable.ic_approved_stamp);
    } else  {
        viewHolder.stampView.setImageResource(R.drawable.ic_approved_stamp_disabled);
    }
    return view;
}


和简单的ViewHolder:

private class ViewHolder {
    public ImageView stampView;

    public ViewHolder(View view)    {
        stampView = (ImageView) view.findViewById(R.id.grid_stamp_image);
    }
}


如果我不得不猜测的话,我会说它失败是因为创建顺序使getView()在onLoadFinished()可以传入模型之前完成。我只是不确定如何解决此问题或我使用了什么其他方法可以用。任何帮助表示赞赏。

最佳答案

对于您想要实现的目标,这对我来说似乎不必要复杂,尤其是双变量循环。为了简单起见,我只需要这样做:

在onLoadFinished()中

int maxPoints = data.getInt(data.getColumnIndex(RewardsEntry.COLUMN_REW_MAX_POINTS));
int currentPoints = data.getInt(data.getColumnIndex(RewardsEntry.COLUMN_REW_POINTS));
mGridView.setAdapter(new RewardPointAdapter(getActivity(), currentPoints, maxPoints);


您的适配器可能如下所示:

public class RewardPointAdapter extends BaseAdapter {
    private LayoutInflater mLayoutInflater;
    private int mMaxRewardPoints;
    private int mCurrentRewardPoints;

    public RewardPointAdapter(Context context, int currentRewardPoints, int maxRewardPoints) {
        mLayoutInflater = LayoutInflater.from(context);
        mMaxRewardPoints = maxRewardPoints;
        mCurrentRewardPoints = currentRewardPoints;
    }

    @Override
    public int getCount() {
        return mMaxRewardPoints;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.list_item_discover, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (position < mCurrentRewardPoints)   {
            viewHolder.stampView.setImageResource(R.drawable.ic_approved_stamp);
        } else  {
            viewHolder.stampView.setImageResource(R.drawable.ic_approved_stamp_disabled);
        }
        return convertView;
    }

    private class ViewHolder {
        public ImageView stampView;

        public ViewHolder(View view)    {
            stampView = (ImageView) view.findViewById(R.id.grid_stamp_image);
        }
    }

}

07-24 09:16