GridView中的Android图像顺序错误

时间:2015-05-18 15:36:34

标签: android arrays gridview random imageview

我的图像gridView有问题。 我将所有图像放在一个数组中,但是当显示网格时,图像不是我决定的顺序。 这是我从各种教程中获取的代码。

主要活动:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Clicca sull'immagine per ingrandire"
    android:id="@+id/textView2"
    android:layout_gravity="center_horizontal"
    android:textAlignment="center"
    android:paddingBottom="10sp"
    android:textColor="@color/white"
    android:textSize="@dimen/text_size"/>

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/grid_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:numColumns="auto_fit"
    android:columnWidth="300dp"
    android:horizontalSpacing="10dp"
    android:verticalSpacing="10dp"
    android:gravity="center"
    android:stretchMode="columnWidth" >
</GridView>

相关的java代码:

package ...;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

public class  extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_);

    GridView gridView = (GridView) findViewById(R.id.grid_view);

    // Instance of ImageAdapter Class
    gridView.setAdapter(new ImageAdapter(this));

    /**
     * On Click event for Single Gridview Item
     * */
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {

            // Sending image id to FullScreenActivity
            Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
            // passing array index
            i.putExtra("id", position);
            startActivity(i);
        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

图像适配器:

package com.example.valeria.Flexibilia_abbigliamento_per_danze_caraibiche_by_DDLAB;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

// Keep all Images in array
public Integer[] mThumbIds = {
        R.drawable.max27a1ridotta, R.drawable.max27b1ridotta,
        R.drawable.max27c1ridotta, R.drawable.max29a1ridotta,
        R.drawable.max29b1ridotta, R.drawable.max30a1ridotta,
        R.drawable.max30b1ridotta, R.drawable.max33a1ridotta,
        R.drawable.max33b1ridotta, R.drawable.max36a1ridotta,
        R.drawable.max38a1ridotta, R.drawable.max38b1ridotta,
        R.drawable.max281ridotta, R.drawable.max311ridotta,
        R.drawable.max321ridotta, R.drawable.max341ridotta,
        R.drawable.max351ridotta, R.drawable.max371ridotta,
        };

// Constructor
public ImageAdapter(Context c) {
    mContext = c;
}

@Override
public int getCount() {
    return mThumbIds.length;
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {
        imageView = new ImageView(mContext);
        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imageView.setLayoutParams(new GridView.LayoutParams(350, 300));
    } else {
        imageView = (ImageView) convertView;
    }

    return imageView;
}
}

gridView显示正确的项目数,但图像是随机的。 我是android和编程的新手,并且有可能从各种来源中提取示例我放了太多代码。 我哪里出错了?

谢谢,对不起我的英文......

1 个答案:

答案 0 :(得分:4)

问题不在于图像的顺序,问题在于您使用convertView的方式。即使它不是null你必须填充它,否则你将得到一个缓存的View for different position。所以这段代码应该可以解决问题:

        if (convertView == null) {
            imageView = new ImageView(mContext);
            imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            imageView.setLayoutParams(new GridView.LayoutParams(350, 300));
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setImageResource(mThumbIds[position]);
        return imageView;

就未来而言,我建议你阅读ViewHolder patern