如何让我的代码从随机生成的食物中选择?

时间:2017-03-09 06:35:03

标签: java android android-studio android-fragments random

当我按下我的按钮时,我希望它能够从7个亚洲地区中的一个随机选择食物。当我的随机数出现时:2我打印出来说

  

"今天你将在null"。

如何打印出餐馆的名称并将其链接到新的java页面以显示它的信息?

这是我声明我的数组的地方:

 private void createResArray() {
    Restaurant chacha = new Restaurant("Cha Cha", FoodCategory.Asian);
    Restaurant sammy = new Restaurant("Sammy Chom's K-Town", FoodCategory.Asian);
    Restaurant sushianddinsum = new Restaurant("Sushi & Dinsum", FoodCategory.Asian);
    Restaurant masterwok = new Restaurant("Master Wok", FoodCategory.Asian);
    Restaurant chinastar = new Restaurant("China Star", FoodCategory.Asian);
    Restaurant phohasaigon = new Restaurant("Pho Ha Saigon", FoodCategory.Asian);


    Restaurant taiAli = new Restaurant("Tai’s Ali's Middle East Food", FoodCategory.Middle_Eastern);
    Restaurant aliMiddle = new Restaurant("Ali's Middle East Food", FoodCategory.Middle_Eastern);
    Restaurant nycHalal = new Restaurant("NYC Halal", FoodCategory.Middle_Eastern);

    resArray = new SparseArray<Restaurant>();

    resArray.append(0, chacha);
    resArray.append(1, sammy);
    resArray.append(2, sushianddinsum);
    resArray.append(3, masterwok);
    resArray.append(4, chinastar);
    resArray.append(5, phohasaigon);


    resArray.append(6, taiAli);
    resArray.append(7, aliMiddle);
    resArray.append(8, nycHalal);
    resCount = 9;

这是我的随机数:

 private void pickRestaurant(int checkedRadioButtonId) {
    Random r = new Random();
    int randomNumber = -1;

    switch (checkedRadioButtonId) {
        case R.id.Asian:
            randomNumber = r.nextInt(7)+1 ;
            break;
        case R.id.middle_eastern:
            randomNumber = r.nextInt() ;
            break;

}

    String randomRestaurantName = resArray.get(randomNumber).getName();
    Toast toast = null;
    if (randomNumber < 0) {
        toast = Toast.makeText(mContext, "Select from one of the following:",
                Toast.LENGTH_SHORT);
    } else {
        toast = Toast.makeText(mContext, "Today you will eat at "
                + randomRestaurantName , Toast.LENGTH_LONG);
    }
    toast.setGravity(Gravity.CENTER, 0, 0);
    toast.show();
}

}

Resturant.java

public class Restaurant {
private String name;
FoodCategory foodCategory;
public String getName;

public Restaurant(String name, FoodCategory priceRange) {
    this.name = name;
    this.foodCategory = priceRange;
}

public String getName()
{
    return getName;
}

protected FoodCategory getFoodCategory()
{
    return foodCategory;
}

}

2 个答案:

答案 0 :(得分:3)

您需要覆盖toString()类中的Restaurant方法,以使其返回更合理的内容。

答案 1 :(得分:2)

您实际上是在尝试打印整个Restaurant对象。您只想打印名称。不知道你的餐厅课程是怎么样的,但是如果它会是这样的:

public class Restaurant
{
    String name;
    FoodCategory category;

    public Restaurant(String name, FoodCategory category)
    {
        this.name = name;
        this.category = category;
    }

    public String getName()
    {
        return name;
    }
}

然后你可以这样做:

String randomRestaurantName = resArray.get(randomNumber).getName();