方向改变时图像为空白?

时间:2011-03-23 09:13:35

标签: android gridview adapter

我想问一下为什么我的网格视图无法在方向更改后显示图像

我设置了onConfigurationChange()和onRetainNonCofigurationInstance()但没有解决我的问题。

这是我的代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.category_layout);

    data = getLastNonConfigurationInstance();

    setUpView();


}

private void setUpView() {
    gridview = (GridView) findViewById(R.id.gridlayout);
    if(data == null){
        imageGridViewAdapter = new ImageGridViewAdapter(this, category, image_category);
    }
    else{
        imageGridViewAdapter = (ImageGridViewAdapter) data;
    }
    gridview.setAdapter(imageGridViewAdapter);

    gridview.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1,
                final int arg2, long arg3) {

            if (subCategory[arg2].length > 1) {
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        CategoryActivity.this);
                builder.setTitle(category[arg2]);
                builder.setItems(subCategory[arg2],
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int item) {
                                linkNewsCategory(arg2, item);
                            }
                        });
                builder.show();
            } else {
                linkNewsCategory(arg2, 0);
            }

        }

    });
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
      setUpView();
}



@Override
public ImageGridViewAdapter onRetainNonConfigurationInstance() {
    ImageGridViewAdapter a = new ImageGridViewAdapter(this, category, image_category);
    return a;
}

是gridview的错误吗?

1 个答案:

答案 0 :(得分:0)

最后我找到了解决方案.. 我在设置图像和文字时有错误的适配器..

这是我的旧getView():

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        v = mInflater.inflate(R.layout.category, null);
        holder = new ViewHolder();
        holder.category_title = (TextView) v.findViewById(R.id.text);
        holder.category_image = (ImageView) v.findViewById(R.id.image);
        v.setTag(holder);
    } else{
        holder = (ViewHolder) v.getTag();
    }

    holder.category_title.setText(data[position]);
    holder.category_image.setImageResource(image[position]);

    return v;
}

我改变了这个:

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

    v = convertView;
    if(convertView == null){
        LayoutInflater li = activity.getLayoutInflater();
        v = li.inflate(R.layout.category, null);

        category_title = (TextView) v.findViewById(R.id.text);
        category_title.setText(data[position]);

        category_image = (ImageView) v.findViewById(R.id.image);
        category_image.setImageResource(image[position]);

    }
    return v;
}

现在它可以解决我的问题..

感谢