【问题标题】:Code not working (Randomize Array)代码不工作(随机数组)
【发布时间】:2015-02-23 10:06:36
【问题描述】:

嘿,我想寻求帮助,因为我不知道为什么我的代码不起作用。我是 android 编程新手,想寻求帮助。

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


    }

然后……

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;

        }
    }

随机图像不会显示.. 只是具有相同顺序的相同数组

【问题讨论】:

  • 使用当前代码有什么问题?
  • @ρяσѕρєяK 数组未随机化。
  • @CollapsedSounds: 创建loladapter类的对象请显示相关部分代码
  • @CollapsedSounds: 在哪里调用RandomizeArray 方法?
  • @ρяσѕρєяK omg 我愚蠢地称该方法是错误的类。谢谢兄弟,抱歉这个愚蠢的问题

标签: java android arrays random


【解决方案1】:
public static int[] RandomizeArray(int[] images){
       int[] img=images;
       ArrayList<Integer> image= new ArrayList<Integer(Arrays.asList(img));
       Collections.shuffle(image);

        return images;
    }

【讨论】:

    【解决方案2】:

    randomizeArray 方法中,您获取一个数组并创建一个完全不同的数组实例。然后你(大概)获取这个新实例并将其存储在一个变量中,而适配器仍然保存原始数组。

    方案一:修改原数组

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

    来源:https://stackoverflow.com/a/1520212/2444099

    在你执行randomizeArray之后调用这个来更新相关的列表视图:

    mAdapter.notifyDataSetChanged();
    

    选项 2:更新适配器中的引用

    让适配器有更新图片的方法:

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

    获取新的打乱数组后调用此方法。

    超出范围

    由于int[] images 仅与适配器类相关,也许您应该将定义移至适配器类并使适配器类静态以避免潜在的内存泄漏。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 2012-11-08
      • 2011-09-04
      • 1970-01-01
      • 1970-01-01
      • 2011-05-02
      • 2015-04-08
      相关资源
      最近更新 更多