1

可変長または 12 の長さの画像のクリック可能なグリッドビューを生成したいと考えています。これはうまくいきましたが、重複したペア (6 ペア) の画像 (果物など) を (リストから) ランダムに選択したい [一致するサウンドを、クリックされたときに画像/グリッドビューの位置に接続する必要もあります] . それらを配列に追加する必要がありますか? クリックされたときに一致するサウンドを写真/グリッドビューの位置に接続する必要があります。

そのように配列を設定できますか? コマンドは何ですか?たとえば、この方法でarray1に追加できますか:

array1[n] = arraycontainingimages[randomnumbern];

? 私の選択肢は何ですか、それとも最善の方法は何ですか? null 配列を作成してから画像を追加できますか? それらを追加できるコマンドはどれですか? ArrayList() Java コマンドを使用してから add(int index, Object element) を使用したほうがよいでしょうか。クリックしたら、グリッドビューからアイテムを削除する必要もあります。

以下にいくつかのサンプル コードを示しますが、初心者のため、さまざまな点で間違っている可能性があります。私が過度に複雑である場合は、お知らせください。

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

        }
    });
}

}

4

1 に答える 1

0

明らかに、私は1つの投稿であまりにも多くの質問をしました。二度と起こらない。簡単な答えは次のとおりです。

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.....

私が発見したように2つのアレイリストを使用するのではなく、サウンドを画像に一致させるには、次のようにします。

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

反対の記事にもかかわらず、同じシードを使用した場合でも、両方のアレイで同じ順序を維持しませんでした。

3クリック時にアイコンを変更するには問題があったため、画像を変更した後にグリッド全体を更新しました。

MyGridviewAdapter1.notifyDataChanged();
gridview.invalidateViews();
于 2012-05-18T09:50:26.577 に答える