Android GridView错误

时间:2012-12-24 15:23:06

标签: android android-layout android-gridview android-gridlayout

我有一个带GridView的应用程序,它包含60个元素......这是ImageAdapter的代码,它扩展了BaseAdapter以显示它们......

public class ImageAdapter extends BaseAdapter {
private Context context;
private final String[] mobileValues;

public ImageAdapter(Context context, String[] mobileValues) {
    this.context = context;
    this.mobileValues = mobileValues;
}

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

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View gridView;

    if (convertView == null) {

        gridView = new View(context);

        // get layout from mobile.xml
        gridView = inflater.inflate(R.layout.mobile, null);

        // set value into textview
        TextView textView = (TextView) gridView
                .findViewById(R.id.grid_item_label);
        textView.setText(mobileValues[position]);

        // set image based on selected text
        ImageView imageView = (ImageView) gridView
                .findViewById(R.id.grid_item_image);

        String mobile = mobileValues[position];

        if (mobile.equals("Alfa Romeo")) {
            imageView.setImageResource(R.drawable.alfa);
        } else if (mobile.equals("Aston Martin")) {
            imageView.setImageResource(R.drawable.aston);
        } else if (mobile.equals("Audi")) {
            imageView.setImageResource(R.drawable.audi);
        } else if (mobile.equals("Bentley")) {
            imageView.setImageResource(R.drawable.bentley);
        } else if (mobile.equals("BMW")) {
            imageView.setImageResource(R.drawable.bmw);
        } else if (mobile.equals("Bugatti")) {
            imageView.setImageResource(R.drawable.bugatti);
        } else if (mobile.equals("Buick")) {
            imageView.setImageResource(R.drawable.buick);
        } else if (mobile.equals("Cadillac")) {
            imageView.setImageResource(R.drawable.cadillac);
        } else if (mobile.equals("Chery")) {
            imageView.setImageResource(R.drawable.chery);
        } else if (mobile.equals("Chevrolet")) {
            imageView.setImageResource(R.drawable.chevrolet);
        } else if (mobile.equals("Chrysler")) {
            imageView.setImageResource(R.drawable.chrysler);
        } else if (mobile.equals("Citroen")) {
            imageView.setImageResource(R.drawable.citroen);
        } else if (mobile.equals("Dacia")) {
            imageView.setImageResource(R.drawable.dacia);
        } else if (mobile.equals("Daewoo")) {
            imageView.setImageResource(R.drawable.daewoo);
        } else if (mobile.equals("Dodge")) {
            imageView.setImageResource(R.drawable.dodge);
        } else if (mobile.equals("Ferrari")) {
            imageView.setImageResource(R.drawable.ferrari);
        } else if (mobile.equals("Fiat")) {
            imageView.setImageResource(R.drawable.fiat);
        } else if (mobile.equals("Ford")) {
            imageView.setImageResource(R.drawable.ford);
        } else if (mobile.equals("Gaz")) {
            imageView.setImageResource(R.drawable.gaz);
        } else if (mobile.equals("Holden")) {
            imageView.setImageResource(R.drawable.holden);
        } else if (mobile.equals("Honda")) {
            imageView.setImageResource(R.drawable.honda);
        } else if (mobile.equals("Hyindai")) {
            imageView.setImageResource(R.drawable.hyundai);
        } else if (mobile.equals("Infinity")) {
            imageView.setImageResource(R.drawable.infiniti);
        } else if (mobile.equals("Jaguar")) {
            imageView.setImageResource(R.drawable.jaguar);
        } else if (mobile.equals("Jeep")) {
            imageView.setImageResource(R.drawable.jeep);
        } else if (mobile.equals("Kia")) {
            imageView.setImageResource(R.drawable.kia);
        } else if (mobile.equals("Lada")) {
            imageView.setImageResource(R.drawable.lada);
        } else if (mobile.equals("Lamborghini")) {
            imageView.setImageResource(R.drawable.lamborghini);
        } else if (mobile.equals("Lancia")) {
            imageView.setImageResource(R.drawable.lancia);
        } else if (mobile.equals("Land Rover")) {
            imageView.setImageResource(R.drawable.land);
        } else if (mobile.equals("Lexus")) {
            imageView.setImageResource(R.drawable.lexus);
        } else if (mobile.equals("Lotus")) {
            imageView.setImageResource(R.drawable.lotus);
        } else if (mobile.equals("Maserati")) {
            imageView.setImageResource(R.drawable.maserati);
        } else if (mobile.equals("Maybach")) {
            imageView.setImageResource(R.drawable.maybach);
        } else if (mobile.equals("Mazda")) {
            imageView.setImageResource(R.drawable.mazda);
        } else if (mobile.equals("Mercedes")) {
            imageView.setImageResource(R.drawable.mercedes);
        } else if (mobile.equals("Mercuri")) {
            imageView.setImageResource(R.drawable.mercury);
        } else if (mobile.equals("Mini")) {
            imageView.setImageResource(R.drawable.mini);
        } else if (mobile.equals("Mitshubishi")) {
            imageView.setImageResource(R.drawable.mitsubishi);
        } else if (mobile.equals("Nissan")) {
            imageView.setImageResource(R.drawable.nissan);
        } else if (mobile.equals("Opel")) {
            imageView.setImageResource(R.drawable.opel);
        } else if (mobile.equals("Pagani")) {
            imageView.setImageResource(R.drawable.pagani);
        } else if (mobile.equals("Peugeot")) {
            imageView.setImageResource(R.drawable.peugeot);
        } else if (mobile.equals("Pontiac")) {
            imageView.setImageResource(R.drawable.pontiac);
        } else if (mobile.equals("Porshe")) {
            imageView.setImageResource(R.drawable.porshe);
        } else if (mobile.equals("Renault")) {
            imageView.setImageResource(R.drawable.renault);
        } else if (mobile.equals("Rolls Royce")) {
            imageView.setImageResource(R.drawable.rolls);
        } else if (mobile.equals("Rover")) {
            imageView.setImageResource(R.drawable.rover);
        } else if (mobile.equals("Saab")) {
            imageView.setImageResource(R.drawable.saab);
        } else if (mobile.equals("Scion")) {
            imageView.setImageResource(R.drawable.scion);
        } else if (mobile.equals("Seat")) {
            imageView.setImageResource(R.drawable.seat);
        } else if (mobile.equals("Skoda")) {
            imageView.setImageResource(R.drawable.skoda);
        } else if (mobile.equals("Sssang Young")) {
            imageView.setImageResource(R.drawable.ssang);
        } else if (mobile.equals("Subaru")) {
            imageView.setImageResource(R.drawable.subaru);
        } else if (mobile.equals("Suzuki")) {
            imageView.setImageResource(R.drawable.suzuki);
        } else if (mobile.equals("Toyota")) {
            imageView.setImageResource(R.drawable.toyota);
        } else if (mobile.equals("Vauxhall")) {
            imageView.setImageResource(R.drawable.vauxhall);
        } else if (mobile.equals("Volkswagen")) {
            imageView.setImageResource(R.drawable.volkswagen);
        } else if (mobile.equals("Volvo")) {
            imageView.setImageResource(R.drawable.volvo);
        } else {
            imageView.setImageResource(R.drawable.acura);
        }

    } else {
        gridView = (View) convertView;
    }

    return gridView;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return mobileValues.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
  }
 } 

问题是 - 当我向下滚动网格视图时 - 它从一开始就开始显示元素....我的错误是什么????

1 个答案:

答案 0 :(得分:4)

您没有正确使用convertView。适配器中的视图将被回收,以防止额外的视图创建。如果视图被回收,则会在convertView中传递。但重要的是,视图以前可能属于不同的项目。如果您不再次填充视图,它将包含不正确的数据 - 属于其他已回收项目的数据。

getView方法应包含以下内容:

public View getView(int position, View convertView, ViewGroup parent) {
    View gridView;

    if (convertView == null) {
        // create new gridView here
    } else {
        // recycle the convertView
        gridView = (View) convertView;
    }

    // fill the gridView here

    return gridView;
}