-1

Hey i wanted to ask for help since i dont know why my code is not working. Im new to android programming and wanted to ask for help.

    public class MotivationalQuotesMenu extends Activity {

    int [] images = {R.drawable.mem1, R.drawable.mem2, R.drawable.mem3};

    public static int[] RandomizeArray(int[] images){
            Random rgen = new Random();  // Random number generator

            for (int i=0; i<images.length; i++) {
                int randomPosition = rgen.nextInt(images.length);
                int temp = images[i];
                images[i] = images[randomPosition];
                images[randomPosition] = temp;
            }

            return images;
        }

@Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.motivationalquotesmenu);

        Resources res = getResources();
        memetitles = res.getStringArray(R.array.omg);


        list = (ListView) findViewById(R.id.listView);
        loladapter adapter = new loladapter(this, memetitles, images);
        list.setAdapter(adapter);


    }

And then...

class loladapter extends ArrayAdapter<String> {
        Context context;
//class that shows a specific row in listview

        private loladapter(Context c, String[] titles, int imgs[]) {


            super(c, R.layout.singlerow, R.id.textView, titles);
            this.context = c;
            images = imgs;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = inflater.inflate(R.layout.singlerow, parent, false);
            ImageView myimageie = (ImageView) row.findViewById(R.id.imageView);

            myimageie.setImageResource(images[position]);
            return row;

        }
    }

Random images wont show.. just the same array with the same order

2 Answers2

1
public static int[] RandomizeArray(int[] images){
       int[] img=images;
       ArrayList<Integer> image= new ArrayList<Integer(Arrays.asList(img));
       Collections.shuffle(image);

        return images;
    }
Seema Nagar
  • 91
  • 1
  • 6
0

In the randomizeArray method you take an array in and create a completely different instance of array. Then you (presumably) take this new instance and store it in a variable while the adapter still holds the original array.

Option 1: Modify the original array

public static void randomizeArray(int[] ar) {
    Random rnd = new Random();
    for (int i = ar.length - 1; i > 0; i--) {
        int index = rnd.nextInt(i + 1);
        // Simple swap
        int a = ar[index];
        ar[index] = ar[i];
        ar[i] = a;
    }
}

Source: https://stackoverflow.com/a/1520212/2444099

After you execute randomizeArray call this to update the associated list view:

mAdapter.notifyDataSetChanged();

Option 2: Update the reference in the adapter

Let the adapter have a method to update images:

public void updateImages(int[] newImages) {
    images = newImages;
    notifyDataSetChanged();
}

Call this method after you obtain new shuffled array.

Out of the scope

Since the int[] images is only relevant to the adapter class, maybe you should move the definition to the adapter class and make the adapter class static to avoid potential memory leaks.

Community
  • 1
  • 1
Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124