drawables和if语句的数组不会改变

时间:2014-07-21 22:57:02

标签: android arrays imageview

我有一个随机类,在开始我的活动时打开一个随机ImageView。每次打开随机图像时,文本都是“بصل”

public class GiftsAct extends Activity {

ImageView im;
int imagesOfGifts [] = {
        R.drawable.thebasal,
        R.drawable.theboot,
        R.drawable.thecamera,
        R.drawable.thehandbag,
        R.drawable.thelap,
        R.drawable.thephone,
        R.drawable.thewatch,
};
Random imagesChoosing = new Random();
TextView greetingMessage;
TextView nameOfUser;
TextView giftName;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gifts);



    im = (ImageView)findViewById(R.id.imageView1);
    int any = imagesChoosing.nextInt(7);
    im.setImageResource(imagesOfGifts[any]);
    giftName = (TextView)findViewById(R.id.giftNAme);
    greetingMessage = (TextView)findViewById(R.id.theTextOfGreeting);
    nameOfUser = (TextView)findViewById(R.id.NameOfThePersonIntent);

   /** // Font path
    String fontPath = "fonts/kharabeesh.ttfy";

    // Loading Font Face
    Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);

    // Applying font
    greetingMessage.setTypeface(tf);
    nameOfUser.setTypeface(tf);
    **/

    Intent getSomeThing = getIntent();
    String valsOfName =  getSomeThing.getStringExtra("theName");
    nameOfUser.setText(valsOfName);



    if (imagesOfGifts[0] == R.drawable.thebasal)
        giftName.setText("بصل");

    else if (imagesOfGifts[1] == R.drawable.theboot)
        giftName.setText("شبشب");

    else if (imagesOfGifts[2] == R.drawable.thecamera)
        giftName.setText("كاميرا");

    else if (imagesOfGifts[3] == R.drawable.thehandbag)
        giftName.setText("شنطة نسائية");

    else if (imagesOfGifts[4] == R.drawable.thelap)
        giftName.setText("لابتوب");

    else if (imagesOfGifts[5] == R.drawable.thephone)
        giftName.setText("ايفون");

    else if (imagesOfGifts[6] == R.drawable.thewatch)
        giftName.setText("ساعة");





}

2 个答案:

答案 0 :(得分:0)

下面...

int imagesOfGifts [] = {
        R.drawable.thebasal,
        R.drawable.theboot,
        R.drawable.thecamera,
        R.drawable.thehandbag,
        R.drawable.thelap,
        R.drawable.thephone,
        R.drawable.thewatch,
};

你有一个包含这些元素的数组,这些元素在我看来并没有改变。

然后你if元素......

if (imagesOfGifts[0] == R.drawable.thebasal)
    giftName.setText("بصل");

imagesOfGifts[0]总是== R.drawable.thebasal

IOW,你可能在视图中随机化图像而不是数组中的元素,因此你总是满足第一个if

你可以switch随机索引摆脱if链......如

switch(any) {
    case 0:
        giftName.setText("بصل");
        break;
    // continue with other cases
}

或者像现在这样做......

if(any == 0)
    giftName.setText("بصل");   
else if ...

而不是......

if (imagesOfGifts[0] == R.drawable.thebasal)
    giftName.setText("بصل");

答案 1 :(得分:0)

您可以使用稀疏的int数组(相当于int hashmap或键值对)来执行您尝试执行的操作。您应该将字符串外部化到strings.xml中,然后可以像这样创建一个稀疏的int数组。

public class GiftsAct extends Activity{

  private static SparseIntArray gifts;

  protected void onCreate(Bundle savedInstanceState) {

     gifts  = new SparseIntArray();
     gifts.append(R.drawable.thebasal, R.string.thebasal);
     gifts.append(R.drawable.theboot, R.string.theboot);
     //ect...
     //your code
     im.setImageResource(gifts.keyAt(any));
     // more of your code
     giftName.setText(gifts.valueAt(any));

   }
}

这样你就不必使用大量的if语句,你可能会找到一种通过循环动态构建数组的方法。