-1

I'm working in Java code, but I can not figure out how to shuffle my images and text. Here is all of my coding. They are in different classes so I will post them separately. This is in Android Studio.

This is my ImageandTextAdapter code:

public class ImageAndTextAdapter extends ArrayAdapter<String> {

    private LayoutInflater mInflater;

    private  String[] mStrings;
    private TypedArray mIcons;

    private int mViewResourceId;

    public ImageAndTextAdapter (Context ctx, int ViewResourceId, String[] strings, TypedArray icons) {
        super(ctx, ViewResourceId, strings);

     mInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

     mStrings = strings;
     mIcons = icons;

     mViewResourceId = ViewResourceId;
    }

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

    @Override
    public String getItem(int position) {return mStrings[position]; }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = mInflater.inflate(mViewResourceId, null);

        ImageView iv = (ImageView) convertView.findViewById(R.id.imageView);

        iv.setImageDrawable(mIcons.getDrawable(position));

        TextView tv = (TextView)convertView.findViewById(R.id.textView);
        tv.setText(mStrings[position]);

        return convertView;
    }

}

This is my picture array code:

<resources>
<array name="card_faces">
    <item>@drawable/b01</item>
    <item>@drawable/b02</item>
    <item>@drawable/b03</item>
    <item>@drawable/b04</item>
    <item>@drawable/b05</item>
    <item>@drawable/b06</item>
    <item>@drawable/b07</item>
    <item>@drawable/b08</item>

</array>
</resources>

This is my main activity code:

public class MainActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Context context = getApplicationContext();
        Resources resources = context.getResources();

        String[] card = {"Blue1", "Blue2", "BlueSnake", "Blue4", "Blue5", "Blue6", "Blue7", "BlueHawk"};

        TypedArray card_faces = resources.obtainTypedArray(R.array.card_faces);

        setListAdapter(new ImageAndTextAdapter (context, R.layout.secondary_layout, card, card_faces));

    }
}

I want to shuffle my pictures which are set in the array, but I am not able to with the Collections.shuffle command. If you know of a way that I could shuffle my pictures using a different and simple method, please help.

esubes21
  • 1
  • 1

1 Answers1

0

You can do it by converting the array to list and shuffle and then convert back to array

String[] card = {"Blue1", "Blue2", "BlueSnake", "Blue4", "Blue5", "Blue6", "Blue7", "BlueHawk"};
List<String> cardList = Arrays.asList(card);
Collections.shuffle(cardList);
cardList.toArray(card);

if you don't want to use more space you can try using random number generator to shuffle manually.

private static <T> void shuffle(T[] input){
    Random rand = new Random();
    for (int i = 0; i < input.length; i++) {
        swap(input, i, rand.nextInt(input.length));
    }
}
private static <T> void swap(T[] input, int x, int y){
    T tmp = input[x];
    input[x] = input[y];
    input[y] = tmp;
}

Edit

If you want to shuffle the images which are inside a TypedArray, Instead of shuffling the string you can simply create an array of int with index and shuffle them then use its position for both string and images.

don't shuffle the string

List<Integer> indexes = new ArrayList<>();
for(int i = 0; i < indexes.length; i++){
    indexes.add(i);
}
Collections.shuffle(indexes);

While using

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = mInflater.inflate(mViewResourceId, null);

        int newPosition = indexes.get(position);
        ImageView iv = (ImageView) convertView.findViewById(R.id.imageView);

        iv.setImageDrawable(mIcons.getDrawable(newPosition));

        TextView tv = (TextView)convertView.findViewById(R.id.textView);
        tv.setText(mStrings[newPosition]);

        return convertView;
    }
Kavin Eswaramoorthy
  • 1,595
  • 11
  • 19
  • What this is doing is shuffling the wording in the code. When I run my application on the device in Android Studio, I will have an image and the text next to the image. When I run this code I am shuffling my text and not my images. Any idea on how to reverse the code to shuffle my images? – esubes21 May 15 '20 at 00:44
  • @esubes21 see the updated answer – Kavin Eswaramoorthy May 16 '20 at 05:17