-3
package walmart.namespace;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class WalmartActivity extends Activity {
        /** Called when the activity is first created. */

        EditText department;
        EditText name;
        Button search;
        String display;

        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                department = (EditText) findViewById(getResources().getIdentifier(
                                "etNum", "id", getPackageName()));
                name = (EditText) findViewById(getResources().getIdentifier("etName",
                                "id", getPackageName()));
                search = (Button) findViewById(getResources().getIdentifier(
                                "btnSearch", "id", getPackageName()));
                final String Display;
                switch (department) {
                case 1:
                        display = "food";
                case 2:
                        display = "Health and Beauty Aids";
                case 3:
                        display = "Stationary and Books";
                case 4:
                        display = "Paper Goods";
                case 5:
                        display = "Home Entertainment";
                case 6:
                        display = "Cameras and Film";
                case 7:
                        display = "Toys";
                case 8:
                        display = "Pets and Pet Supplies";
                case 9:
                        display = "Sporting Goods";
                case 10:
                        display = "Automotive";
                case 11:
                        display = "Hardware";
                case 12:
                        display = "Paint and Accessories";
                case 13:
                        display = "Household Chemicals";
                case 14:
                        display = "Housewares";
                case 15:
                        display = "Appliances";
                case 16:
                        display = "Lawn and Garden";
                case 17:
                        display = "Home Furnishings";
                case 18:
                        display = "Seasonal";
                case 19:
                        display = "Fabrics and Crafts";
                case 20:
                        display = "Domestic Goods";
                case 21:
                        display = "Curtains";
                case 22:
                        display = "Bedding";
                case 23:
                        display = "Mens";
                case 24:
                        display = "Boys";
                case 25:
                        display = "Shoes";
                case 26:
                        display = "Infants and Toddlers";
                case 27:
                        display = "Ladies Socks";
                case 28:
                        display = "Nylon Hosiery";
                case 29:
                        display = "Lingerie";
                case 30:
                        display = "Foundations";
                case 31:
                        display = "Handbags and Accessories";
                case 32:
                        display = "Jewlery";
                case 33:
                        display = "Girls";
                case 34:
                        display = "Ladies";
                case 36:
                        display = "Outerwear";
                case 39:
                        display = "Food Services";
                case 40:
                        display = "Pharmacy";
                case 46:
                        display = "Cosmetics";
                case 49:
                        display = "Optical";
                case 56:
                        display = "Horticulture";
                case 58:
                        display = "Income";
                case 77:
                        display = "Large Appliances";
                case 80:
                        display = "Service Deli";
                case 81:
                        display = "Bread";
                case 82:
                        display = "Impulse Buys";
                case 83:
                        display = "Seafood";
                case 84:
                        display = "Floral";
                case 85:
                        display = "1 Hour Photo";
                case 86:
                        display = "Fresh Flowers and Balloons";
                case 87:
                        display = "Wireless";
                case 89:
                        display = "Travel Center";
                case 90:
                        display = "Dairy";
                case 91:
                        display = "Frozen Foods";
                case 92:
                        display = "Grocery";
                case 93:
                        display = "Meats";
                case 94:
                        display = "Produce";
                case 95:
                        display = "DSD Grocery";
                case 96:
                        display = "Liqour";
                case 97:
                        display = "Processed Meats";
                case 98:
                        display = "Bakery";
                case 99:
                        display = "Store Use";
                }

                if (name.equals("Electronics")) {
                        display = "5";
                } else if (name.equals("candy")) {
                        display = ("1");
                } else if (name.equals("Tobacco")) {
                        display = ("1");
                } else if (name.equals("Makeup")) {
                        display = ("2");
                } else if (name.equals("Stationary")) {
                        display = ("3");
                } else if (name.equals("Books")) {
                        display = ("3");
                } else if (name.equals("Napkins")) {
                        display = ("4");
                } else if (name.equals("Paper Plates")) {
                        display = ("4");
                } else if (name.equals("Paper")) {
                        display = ("4");
                } else if (name.equals("Video Games")) {
                        display = ("5");
                } else if (name.equals("Movies")) {
                        display = ("5");
                } else if (name.equals("Cameras")) {
                        display = ("6");
                } else if (name.equals("Film")) {
                        display = ("6");
                } else if (name.equals("Photo")) {
                        display = ("6");
                } else if (name.equals("Toys")) {
                        display = ("7");
                } else if (name.equals("Fishing Rods")) {
                        display = ("9");
                } else if (name.equals("Sporting Equipment")) {
                        display = ("9");
                } else if (name.equals("Sporting Goods")) {
                        display = ("9");
                } else if (name.equals("Car Stereos")) {
                        display = ("10");
                } else if (name.equals("Automotive")) {
                        display = ("10");
                } else if (name.equals("Tools")) {
                        display = ("11");
                } else if (name.equals("Heaters")) {
                        display = ("11");
                } else if (name.equals("Fans")) {
                        display = ("11");
                } else if (name.equals("Hardware")) {
                        display = ("11");
                } else if (name.equals("Paint")) {
                        display = ("12");
                } else if (name.equals("Bleach")) {
                        display = ("13");
                } else if (name.equals("Ammonia")) {
                        display = ("13");
                } else if (name.equals("Chemicals")) {
                        display = ("13");
                } else if (name.equals("Towels")) {
                        display = ("14");
                } else if (name.equals("Kitchen Accessories")) {
                        display = ("14");
                } else if (name.equals("Towels")) {
                        display = ("14");
                } else if (name.equals("Appliances")) {
                        display = ("15");
                } else if (name.equals("Plants")) {
                        display = ("16");
                } else if (name.equals("Grills")) {
                        display = ("16");
                } else if (name.equals("Lawn & Garden")) {
                        display = ("16");
                } else if (name.equals("Furniture")) {
                        display = ("17");
                } else if (name.equals("Seasonal")) {
                        display = ("18");
                } else if (name.equals("Fabrics and Crafts")) {
                        display = ("19");
                } else if (name.equals("Domestics")) {
                        display = ("20");
                } else if (name.equals("Curtains")) {
                        display = ("21");
                } else if (name.equals("Bedding")) {
                        display = ("22");
                } else if (name.equals("Mens")) {
                        display = ("23");
                } else if (name.equals("Boys")) {
                        display = ("24");
                } else if (name.equals("Shoes")) {
                        display = ("25");
                } else if (name.equals("Infants")) {
                        display = ("26");
                } else if (name.equals("Ladies Socks")) {
                        display = ("27");
                } else if (name.equals("Pantyhose")) {
                        display = ("28");
                } else if (name.equals("Lingerie")) {
                        display = ("29");
                } else if (name.equals("Purses")) {
                        display = ("31");
                } else if (name.equals("Handbags")) {
                        display = ("31");
                } else if (name.equals("Jewelry")) {
                        display = ("32");
                } else if (name.equals("Girls")) {
                        display = ("33");
                } else if (name.equals("Maternity")) {
                        display = ("35");
                } else if (name.equals("Outerwear")) {
                        display = ("36");
                }

                else if (name.equals("Pharmacy")) {
                        display = ("40");

                } else if (name.equals("Cosmetics")) {
                        display = ("46");
                } else if (name.equals("Optical")) {
                        display = ("49");
                }

                else if (name.equals("Large Appliances")) {
                        display = ("77");
                } else if (name.equals("Deli")) {
                        display = ("80");
                } else if (name.equals("Bread")) {
                        display = ("81");
                } else if (name.equals("Seafood")) {
                        display = ("83");
                } else if (name.equals("Floral")) {
                        display = ("84");
                } else if (name.equals("One Hour Photo")) {
                        display = ("85");
                } else if (name.equals("Fresh Flowers")) {
                        display = ("86");

                } else if (name.equals("Cell Phones")) {
                        display = ("87");
                } else if (name.equals("Travel Center")) {
                        display = ("89");
                } else if (name.equals("Luggage")) {
                        display = ("89");
                } else if (name.equals("Dairy")) {
                        display = ("90");
                } else if (name.equals("Milk")) {
                        display = ("90");
                } else if (name.equals("Frozen Foods")) {
                        display = ("91");
                } else if (name.equals("Frozen")) {
                        display = ("91");
                } else if (name.equals("Grocery")) {
                        display = ("92");
                } else if (name.equals("Meats")) {
                        display = ("93");
                } else if (name.equals("Produce")) {
                        display = ("94");
                } else if (name.equals("Liquor")) {
                        display = ("96");
                } else if (name.equals("Bakery")) {
                        display = ("98");
                }

                department.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                                department.setText("");
                        }
                });
                name.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                                name.setText("");
                        }
                });
                search.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {

                        }
                });
        }
}

我是 JAVA 的新手,但我在第 29 行的“部门”行收到此错误消息。

无法打开 EditText 类型的值。只允许可转换的 int 值或枚举常量。

我不知道如何解决这个问题。有接盘侠吗?

4

6 回答 6

3

在 Java 6 中,switch只需要intor enum。如果您想从 EditText 中获取文本,将其转换为int,然后在 上使用switch,那么您可以执行以下操作:

switch(Integer.parseInt(department.getText().toString())) // Be careful! This could throw an exception if the text doesn't contain a valid integer
于 2012-04-25T07:42:48.943 回答
2

department不是 int 或 enum 值,因此无法打开。

于 2012-04-25T07:39:52.963 回答
2

正如错误所暗示的,您不能将整个对象传递给您的 switch 语句并期望它能够工作。您需要一些在一天结束时会解析为整数值的东西。

你可以在你的EditText类中有一些方法会返回一些整数,然后,你可以做类似的事情switch(departement.getNumericalValue())并使用 switch 来处理它,但除此之外,你不能使用你当前拥有的东西。

于 2012-04-25T07:41:58.873 回答
1

从 Java 7 开始,您可以切换字符串,但不能切换标准对象

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

但是,您使用的是android,因此仍然是java 6

由于您必须使用 6,因此您需要将字符串转换为整数

switch (Integer.parseInt(department.getText())){
    case 1:
         display = "food";
....
于 2012-04-25T07:45:45.310 回答
0

您应该使用getText从您的EditText字段中获取文本值并将其转换为数值..

于 2012-04-25T07:40:44.363 回答
-1

用 switch 语句替换部门变量上的 switch 语句

Integer.parseInt(department.getText().toString())

于 2012-04-25T07:42:54.147 回答