gpt4 book ai didi

java - 使用数组将随机选择的图像添加到可单击的 GridView

转载 作者:行者123 更新时间:2023-12-01 05:21:00 26 4
gpt4 key购买 nike

我想生成可变或 12 长度图像的可点击 GridView 。我管理得很好,但我想随机(从列表中)以重复对(6对)挑选图像(水果或其他)[我还需要将匹配的声音连接到图片/gridview位置,以便在单击它们时] 。我应该将它们添加到数组中吗?我需要将匹配的声音连接到图片/gridview 位置,以便在单击它们时使用。

我可以用这种方式填充数组吗?命令是什么?例如我可以这样添加到我的 array1 中吗:

array1[n] = arraycontainingimages[randomnumbern];

?我的选择是什么或者最好的方法是什么?我可以创建一个空数组然后添加图像吗?哪个命令可以添加它们?我最好使用 ArrayList() java 命令,然后添加(int index, Object element)。我还需要在单击后从 gridview 中删除该项目。

下面有一些示例代码,由于我是初学者,这些代码可能在很多方面都是错误的。如果我过于复杂,请告诉我。

public class MyImageAdapterFruit1 extends BaseAdapter {

int Totalfruit = 12; // this means there are 6 pairs of fruit to select
int fruitstilltoadd = Totalfruit;
int ftaddcnt = 1;
int puthere = 1;
int counttotwo;

private Context mContext;

public MyImageAdapterFruit1(Context c) {
mContext = c;
}

public int getCount() {
return filenames.length;
}

public Object getItem(int position) {
return null;
}

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

// Create array to reference images later
public Integer [] displayfruit = {
R.drawable.blank,
R.drawable.fruit1, R.drawable.fruit2, R.drawable.fruit3,
R.drawable.fruit4, R.drawable.fruit5, R.drawable.fruit6,
R.drawable.fruit7, R.drawable.fruit8, R.drawable.fruit9,
R.drawable.fruit10, R.drawable.fruit11, R.drawable.fruit12,
};

// Create blank array to be populated randomly later - is this right/ok?
private Integer [] filenames;

// CONSIDER USING public void ArrayList (int capacity) {} here instead

//public void whyiseclipseaddingthis() // Don't know why eclipse is adding this bit--------Fill (filenames) array randomly with 6 images in duplicate

{
while (fruitstilltoadd > 0) {

// select Fruittoaddtoarray - the following creates a random number (temprand) between 1 and 12 (if 12 is the total no. of fruit)
// use this to randomly select fruit to add to the array
int restartfruitselection = 0;
int minr = 1;
int maxr = (Totalfruit + 1);
Random temprand = new Random();
int Fruittoaddtoarray = temprand.nextInt(maxr - minr + 1) + minr;
// end "select Fruittoaddtoarray"

// Now check if Fruittoaddtoarray already in array named "filenames"
for (ftaddcnt = 1; ftaddcnt <= Totalfruit; ftaddcnt++) {
if (filenames[Fruittoaddtoarray] == filenames[ftaddcnt]) {
restartfruitselection = 1; break;
//if already in array then start over selecting new fruit else continue
}

if (restartfruitselection == 0) {
// new fruit has been selected successfully so lets add it TWICE to random positions
counttotwo =1;
while (counttotwo <= 2) {

minr = 1;
maxr = (Totalfruit + 1);
temprand = new Random();
int Puthere = temprand.nextInt(maxr - minr + 1) + minr;
// end selection of random Puthere

//if Puthere location is empty then add fruit to that position in the array ...
// ...otherwise select a different place to put the fruit and re-test
if (filenames[Puthere] == null) {
filenames[Puthere] = displayfruit[Fruittoaddtoarray];
counttotwo++;
fruitstilltoadd--;

}
}
}
}
}}

int Fruitleft = 0;

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}

imageView.setImageResource(filenames[position]);
return imageView;
}

}

感谢您的帮助。欢迎任何方面的帮助。

PS 我的 gameonemainscreen.java 文件是

  GridView gridview = (GridView) findViewById(R.id.gridView);
gridview.setAdapter(new MyImageAdapterFruit1(this));

gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

SoundManager.playSound(2, 1);
Toast.makeText(game1mainscreen.this, "" + position, Toast.LENGTH_SHORT).show();

}
});
}

}

最佳答案

显然我在一篇文章中问了太多问题。不会再发生了。简短的答案是:

1 我使用了数组列表而不是数组,因为您无法修改数组。我交换了数组列表中的图像,如下所示:

myarraylist.set(position, R.drawable.newimage); //swap for new image
myarraylist.remove(0); //remove first int (image) from array

FruitToChooseFromImages = new ArrayList<Integer>();
FruitToChooseFromImages.add(R.drawable.image1);
FruitToChooseFromImages.add(R.drawable.image2); // etc to add images to the arraylist

2我用过:

switch (switchused) {
case R.drawable.fruit0:
soundtoplay = 0;
break;
etc.....

将声音与图像相匹配,而不是像我发现的那样使用两个数组列表:

Collections.shuffle(intarraylistofimages, shufflevalueSeed);
Collections.shuffle(intarraylistofnames, shufflevalueSeed);

即使使用相同的种子,两个数组也不会保持相同的顺序,尽管文章相反。

3 要在单击时更改图标,我遇到了问题,因此我在更改图像后刷新了整个网格:

MyGridviewAdapter1.notifyDataChanged();
gridview.invalidateViews();

关于java - 使用数组将随机选择的图像添加到可单击的 GridView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10429823/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com