提问者:小点点

在数组列表中随机更改项的顺序


我有一个数组,它有许多项images。 我想随意改变物品的顺序。 当用户点击按钮时,图像的顺序会随机改变。 有没有什么代码可以做到那一点?。

更多详细信息的一些代码:

/* ArrayList for MoqAlHaramImages */
final String[] MoqAlHaramImages = {
    // 1
    "https://mlpkhhqlr6vc.i.optimole.com/wFyunBg.nEnB~572a0/w:auto/h:auto/q:55/wm:87978:1:sowe:0:0:0.2/https://www.qallwdall.com/wp-content/uploads/2019/02/9.png",
    // 2
    "https://sportnewsps.com/content/uploads/2020/04/28/20ebf2fc33.jpg",
    // 3
    "https://3.bp.blogspot.com/-l49XMcefjRQ/XMm94lY36RI/AAAAAAAAMp0/ty--eWsSA3obK_zGaO_EpEsbFCyuBoHeQCLcBGAs/s640/%25D8%25A8%25D8%25A7%25D9%2582%25D8%25A9%2B%25D9%2588%25D8%25B1%25D8%25AF%2B%25D8%25B5%25D8%25A8%25D8%25A7%25D8%25AD%2B%25D8%25A7%25D9%2584%25D8%25AE%25D9%258A%25D8%25B13.jpg",
    // 4
    "https://pbs.twimg.com/media/BIWmhEgCAAAZQRu.jpg",
    // 5
    "https://2.bp.blogspot.com/-h_SE9HvurL4/XMm93PMMZbI/AAAAAAAAMpo/JwcCvDhP_bUhazc36vS-VU9annd3l7GCgCLcBGAs/s1600/%25D8%25A8%25D8%25A7%25D9%2582%25D8%25A9%2B%25D9%2588%25D8%25B1%25D8%25AF%2B%25D8%25B5%25D8%25A8%25D8%25A7%25D8%25AD%2B%25D8%25A7%25D9%2584%25D8%25AE%25D9%258A%25D8%25B11.jpg",
    // 6
    "https://i.pinimg.com/originals/68/d7/cd/68d7cdbbdfb2939cfb44eeed6d7e9448.jpg",
};

/* make new object and find the view "GridView" */
GridView gridView = findViewById(R.id.gridview_image_haram);
// display all the images from Array on it
gridView.setAdapter(new PicassoImagesAdapter(ImagesMoqAlaramActivity.this, MoqAlHaramImages));

共1个答案

匿名用户

这一行将对您的列表进行洗牌:

Collections.shuffle(Arrays.asList(MoqAlHaramImages));;

如果你还需要点击你的按钮,在这个答案上你会找到所有你需要的:)