Android 2D阵列
我试图根据Android 2D阵列,android,Android,我试图根据单选按钮显示数组的内容。我用了两套按钮。按钮应指向我的阵列中的[x,y]。应显示数组的字符串值(使用Toast) 是的,我在这方面是新手,但我试着用很少的运气来区分几个类似的例子 public class MainActivity extends Activity { String[][] tipSize = { {"N/A","N/A","Pink","Lt Blue","Purple","Yellow","Brown","Orange","Tan",
单选按钮显示数组的内容。我用了两套按钮。按钮应指向我的阵列中的[x,y]
。应显示数组的字符串
值(使用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();
}
}
这条线有很多问题:
tip = String tipSize [disp,o_pg];
[disp,o_pg]
应该是[disp][o_pg]
,因为访问2D数组是通过数组[x][y]
String tipSize
没有意义,您不能在此处声明类型。如果你做了(String)
这将是一个强制转换,这也没关系(但这将是多余的,因为你有一个字符串
数组-从字符串
强制转换到字符串
没有任何作用)
disp
和o_pg
应该是int
这是因为在访问索引时,不能将对象传递给数组,数组不会“搜索”您。这意味着你需要弄清楚你想要进入什么位置,然后把它们传递出去。还要记住,索引从0
开始,而不是从1
开始
一个可编译的例子是:
tip = tipSize [0][0]; //first element of the first array ("N/A")
一种快速而肮脏的方法是创建一个简单的字符串数组,其中包含一个分隔字符串,该字符串将第二个值分组在一起,然后使用split函数,该函数本身返回一个字符串数组来获取各个值
String [] allTips = {"a;b;c", "d;e;f"};
String [] sometips = alltips[1].split(";")
String tip = sometips[2];
会导致字母f
你的问题是什么?我知道语法是错误的,但我留下它是为了尝试显示我正在寻找的方向。感谢您的帮助。如果语法错误,您的编译器错误是什么?当RadioButton传递值时,是@id值还是您可以为每个单选按钮设置值,然后“case check”值?另外,值是作为字符串还是Int传递的?“输入RadioButton”或将其转换为字符串时遇到问题。@user2152750当RadioButton传递值时,请详细说明。什么时候点击?如果RadioButton是RadioGroup的一部分,则可以使用获取RadioButton的Id;如果RadioButton是单独的,则可以使用OnClickListener
。然后,您必须比较id以确定您想要的位置(例如if(R.id.button=view.getId())String s=tipSize[0][0];
此外,如果您发现我的答案有帮助,您应该阅读@user2152750。或者如果您需要进一步澄清(前提是您阅读了我的答案和我发布的链接),请留下评论。