2

アプリに標準の gridview ソースを使用しています。私が直面している問題は、グリッドビューで画像をスクロールすると、画像が毎回動的に変化し、位置に間違った画像があることです。以下の参照用に、私が使用するコードを示します......

  @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Set up an array of the Thumbnail Image ID column we want
    String[] projection = {MediaStore.Images.Thumbnails._ID};
    // Create the cursor pointing to the SDCard
    cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
            projection, // Which columns to return
            null,       // Return all rows
            null,
            MediaStore.Images.Thumbnails.IMAGE_ID);
    // Get the column index of the Thumbnails Image ID
    columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

    GridView gridView = (GridView) findViewById(R.id.gridview);
    gridView.setAdapter(new ImageAdapter(this));




    gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {

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

        }
    });

> ImageAdapter クラス

 private class ImageAdapter extends BaseAdapter {

    private Context context;

    public ImageAdapter(Context localContext) {
        context = localContext;
    }

    public int getCount() {
        return cursor.getCount();
    }
    public Object getItem(int position) {
        return position;
    }
    public long getItemId(int position) {
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView picturesView;
        if (convertView == null) {
            picturesView = new ImageView(context);
            // Move cursor to current position
            cursor.moveToPosition(position);
            // Get the current value for the requested column
            int imageID = cursor.getInt(columnIndex);
            // Set the content of the image based on the provided URI
            picturesView.setImageURI(Uri.withAppendedPath(
                    MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));
            picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            picturesView.setPadding(5, 5, 5, 5);
            picturesView.setLayoutParams(new GridView.LayoutParams(80, 80));
        }
        else {
            picturesView = (ImageView)convertView;
        }
        return picturesView;
    }
}

スクリーンショットの添付、 1.スクロール前 2.下にスクロールし、再度上にスクロール。

スクロール前

もう一度上にスクロールすると

なぜ順番が変わるの??

4

2 に答える 2

5

新しいビューにのみ画像を設定しています。

以下をせよ:

if (convertView == null) {
    picturesView = new ImageView(context);
    picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
    picturesView.setPadding(5, 5, 5, 5);
    picturesView.setLayoutParams(new GridView.LayoutParams(80, 80));    
} else {
    picturesView = (ImageView)convertView;
}
// Move cursor to current position
cursor.moveToPosition(position);
// Get the current value for the requested column
int imageID = cursor.getInt(columnIndex);
// Set the content of the image based on the provided URI
picturesView.setImageURI(Uri.withAppendedPath(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));
于 2012-10-05T09:11:35.167 に答える
2

あなたの問題は、 convertView を使用すると、最初のレコードから古いデータが保存されることです。ビューの変換は、時間とメモリを消費するリソースからのレイアウト インフレーションを回避するために使用されます。古い膨張したビューを使用する必要がありますが、新しいデータを設定してください。

public View getView(int position, View convertView, ViewGroup parent) {  
    ImageView picturesView;  
    if (convertView == null) {  
        picturesView = new ImageView(context);  
    }  
    else {  
        picturesView = (ImageView)convertView;  
    } 
       cursor.moveToPosition(position);  
        // Get the current value for the requested column  
        int imageID = cursor.getInt(columnIndex);  
        // Set the content of the image based on the provided URI  
        picturesView.setImageURI(Uri.withAppendedPath(  
                MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));  
        picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);  
        picturesView.setPadding(5, 5, 5, 5);  
        picturesView.setLayoutParams(new GridView.LayoutParams(80, 80));  
    return picturesView;  
}
于 2012-10-05T09:07:47.583 に答える