我尝试通过点击按钮来重新加载GridView
,以生成并填充新的数字数组到GridView
。这是我的代码没有读取新的数组:
gridView = (GridView) findViewById(R.id.gridView);
final ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this,
android.R.layout.simple_list_item_1,randomArray(arrayNumbers));
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
gridView=(GridView)findViewById(R.id.gridView);
Integer values=Integer.parseInt(gridView.getItemAtPosition(position).toString());
y.setText(String.valueOf(value));
if (a==values) {
Collections.shuffle(randomArray(arrayNumbers));//xao tron vi tri cua cac so trong gridview
adapter.notifyDataSetChanged();//reload lai gridview
a = a + r.nextInt(11);//tang gia tri a
x.setText(String.valueOf(a));
System.out.println(a);
//Todo set gia tri a vao mang random(1) vua tao moi
Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_SHORT).show();
}
}
})
这是我的Randomnumber方法:
public static ArrayList randomArray(int[] a){
Random r= new Random();
List<Integer> list=new ArrayList<>();
for (int j=1;j<25;){
int random=((int)(Math.random()*100))+1;
if (!list.contains(random)){
list.add (random);
j++;
}
}
list.add(30);
return (ArrayList) list;
}
答案 0 :(得分:0)
您可以保留对列表的引用:
numbersArrayList = randomArray(arrayNumbers);
final ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this,
android.R.layout.simple_list_item_1, numbersArrayList);
然后,在不更改引用的情况下,您可以更新OnItemClickListener
中的项目:
numbersArrayList.clear();
List<Integer> newList = randomArray(arrayNumbers);
Collections.shuffle(newList);
numbersArrayList.addAll(newList);
adapter.notifyDataSetChanged();