0

我正在尝试显示基于RadioButtons. 我使用 2 组按钮。该按钮应指向[x, y]我的数组。String应该显示数组的值(使用 a Toast)。

是的,我对此很陌生,但我试图挑选几个类似的例子,但运气不佳。

public class MainActivity extends Activity {
    String[][] tipSize = {
            {"N/A","N/A","Pink","Lt Blue","Purple","Yellow","Brown","Orange","Tan","Blue","White","Beige"},
            {"N/A","Pink","Lt Blue","Purple","Yellow","Brown","Orange","Green","Tan","Blue","White","Beige"},
            {"N/A","N/A","Pink","Purple","Turquoise","Yellow","Green","Tan","Blue","White","Red","No Tip"},
            {"Pink","Lt Blue","Purple","Yellow","Orange","Green","Tan","Blue","Red","Beige","Gray","N/A"},
            {"Pink","Lt Plue","Purple","Orange","Green","Tan","Blue","White","Beige","Black","Gray","N/A"},
            {"Pink","Lt Blue","Yellow","Brown","Orange","Green","Tan","Blue","White","Beige","Black","N/A"},
            {"Pink","Lt Blue","Yellow","Brown","Tan","Blue","White","Red","Beige","Black","No Tip","N/A"},
            {"Pink","Lt Blue","Purple","Yellow","Orange","Green","Tan","Blue","Red","Beige","Gray","N/A"},
    };

private RadioGroup dispenser,ounce_pg;
private RadioButton disp,o_pg;
String tip = "";

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

    Button finishBtn = (Button) findViewById(R.id.button1);
    finishBtn.setOnClickListener (new View.OnClickListener() {      
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            MainActivity.this.showit(); 
        }
    });     
}

protected void showit() {
    // TODO Auto-generated method stub
    disp = (RadioButton) findViewById(R.id.dispenser);
    o_pg = (RadioButton) findViewById(R.id.ounce_pg);
    tip = String tipSize [disp,o_pg];
    // tip is the displayed answer (Color of tip), tipSize[][] is the Array, disp is RadioButton 1 - o_pg is Radio Button 2 values. 

    Toast.makeText(MainActivity(),tip,Toast.LENGTH_LONG).show();
}

}
4

2 回答 2

0

这条线有很多问题:

 tip = String tipSize [disp,o_pg];
  1. [disp,o_pg]应该是[disp][o_pg]因为访问二维数组是通过array [x][y]

  2. String tipSize没有意义,你不能在这里声明一个类型。如果你这样做(String)了,那将是一个演员,那没关系(但由于你有一个数组,这将是多余的-从 aString转换为 a不会做任何事情)。StringString

  3. disp并且o_pg应该是int

这是因为在访问索引时,您不能将 Object 传递给数组,数组不会为您“搜索”。这意味着您需要弄清楚您想要访问哪些职位并将其传递出去。还要记住索引开始于0,而不是1

一个可编译的例子是:

tip = tipSize [0][0]; //first element of the first array ("N/A")
于 2013-03-10T01:03:12.630 回答
0

一种快速而肮脏的方法是创建一个简单的字符串数组,其中包含一个分隔字符串,将第二个值组合在一起,然后使用 split 函数本身返回一个字符串数组来获取各个值。

String [] allTips = {"a;b;c", "d;e;f"};
String [] sometips = alltips[1].split(";")
String tip = sometips[2];

会导致信f

于 2013-03-10T01:09:18.577 回答