数组在gridview中加载两次

时间:2018-04-19 08:37:31

标签: java android gridview

请帮帮我们...... 我卡住了

我有一个像这样的数组

enter image description here 我试图用这个适配器将这个数组放入gridview

public class ImageAdapter extends BaseAdapter {
private Context mContext;
private final String[] intelligences;
private final int[] Imageid;

public ImageAdapter(Context c,String[] intelligences,int[] ImageId){
    this.mContext = c;
    this.intelligences=intelligences;
    this.Imageid=ImageId;
}

< - getcount,getItem ...... - >

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View grid;
    LayoutInflater inflater = (LayoutInflater) 
mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        // if it's not recycled, initialize some attributes
        grid = new View(mContext);
        grid = inflater.inflate(R.layout.custom_grid, null);
        TextView textView = (TextView) grid.findViewById(R.id.textGrid);
        ImageView imageView = (ImageView)grid.findViewById(R.id.imageGrid);
        RelativeLayout rl = 
(RelativeLayout)grid.findViewById(R.id.relative);

        textView.setText(intelligences[position]);
        imageView.setImageResource(Imageid[position]);

        Log.i("------position------",String.valueOf(position));
        switch(position){
            case 0:
            rl.setBackgroundColor(ContextCompat.getColor(mContext, 
R.color.myColor1));
            break;
            case 1:
            rl.setBackgroundColor(ContextCompat.getColor(mContext, 
R.color.myColor2));
            break;
            case 2:
                rl.setBackgroundColor(ContextCompat.getColor(mContext, 
R.color.myColor3));
                break;

        }


    } else {
        grid = (View) convertView;
    }


    return grid;
}

}

,结果就像这样

enter image description here

有两个项目加载两次,一些数据未加载,

提前谢谢 抱歉我的languange ....

2 个答案:

答案 0 :(得分:0)

用这个技巧解决了

if (convertView == null) {
    // if it's not recycled, initialize some attributes
    grid = new View(mContext);
    grid = inflater.inflate(R.layout.custom_grid, null);

} else {
    grid = (View) convertView;
}
TextView textView = (TextView) grid.findViewById(R.id.textGrid);
ImageView imageView = (ImageView) grid.findViewById(R.id.imageGrid);
RelativeLayout rl = (RelativeLayout) grid.findViewById(R.id.relative);

答案 1 :(得分:0)

由于您的实现不正确,请尝试将getView方法更改为:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View grid;
    LayoutInflater inflater = (LayoutInflater)
    mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        // if it's not recycled, initialize some attributes
        grid = new View(mContext);
        grid = inflater.inflate(R.layout.custom_grid, null);
        TextView textView = (TextView) grid.findViewById(R.id.textGrid);
        ImageView imageView = (ImageView)grid.findViewById(R.id.imageGrid);
        RelativeLayout rl = (RelativeLayout)grid.findViewById(R.id.relative);
    } else {
        grid = (View) convertView;
    }

    textView.setText(intelligences[position]);
    imageView.setImageResource(Imageid[position]);

    Log.i("------position------",String.valueOf(position));
    switch(position){
        case 0:
            rl.setBackgroundColor(ContextCompat.getColor(mContext,
                        R.color.myColor1));
            break;
        case 1:
            rl.setBackgroundColor(ContextCompat.getColor(mContext,
                        R.color.myColor2));
            break;
        case 2:
            rl.setBackgroundColor(ContextCompat.getColor(mContext,
                        R.color.myColor3));
            break;
    }

    return grid;
}

然后让我知道结果。