根据数量程序设置GridView的ImageVIew

时间:2014-10-02 06:44:15

标签: java android gridview android-fragments

我试图创建一个图标的Gridview,Imageview设置为两个图像中的一个,如下例所示:

enter image description here

我从sqlite内容提供商处检索两个值,一个用于邮票数量,另一个用于最大邮票数量。

我使用具有视图持有者模式的自定义适配器,因此我尝试在片段中设置模型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);
    }
}

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

1 个答案:

答案 0 :(得分:1)

对于你想要实现的目标,这对我来说似乎不必要,尤其是双变量循环。简单地说,我只是这样做:

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);
        }
    }

}