lundi 18 mai 2015

Android Images in a gridView in wrong order

i have a problem with a image gridView. I put all the images in an array, but when the grid is displayed the images aren’t in the order that I decided. Here is my code taken from various tutorials.

The main activity:

<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://ift.tt/nIICcg"
    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>

Related java code:

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);
}
}

The Image Adapter:

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;
}
}

The gridView display the correct number of items, but the images are randomized. I am new in android and in programming, and is possible that taking examples from various sources I put too much code. Where did I go wrong?

Thanks and sorry for my English…




Aucun commentaire:

Enregistrer un commentaire