2013-03-10 213 views
0

我試圖根據RadioButtons顯示數組的內容。我使用2組按鈕。該按鈕應指向我陣列中的[x, y]。應顯示數組的String值(使用Toast)。Android 2D陣列

是的,我在這方面是新手,但我試圖挑選幾個類似的例子,運氣不大。

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(); 
} 

} 
+0

什麼是你的問題? – 2013-03-10 01:16:12

+0

我知道語法錯了,但我留下來試着展示我看的方向。 感謝您的幫助。 – user2152750 2013-03-10 16:05:21

+0

如果語法錯誤,您的編譯器錯誤是什麼? – 2013-03-10 21:24:59

回答

0

這一行有很多問題:

tip = String tipSize [disp,o_pg]; 
  1. [disp,o_pg]應該是[disp][o_pg]因爲訪問二維數組通過array [x][y]

  2. String tipSize做沒有意義,你可以這裏沒有聲明類型。如果你做了(String)這將是一個演員陣容,這將是好的(但它會是多餘的,因爲你有一個String陣列 - 從String鑄造成String什麼都不做)。

  3. dispo_pg應該是int

這是因爲訪問索引時,你不能將對象傳遞給數組,數組不會「搜索」爲您服務。這意味着你需要弄清楚你想訪問什麼職位並將其傳遞給他人。還要記住索引從0開始,而不是1

可編譯的例子是:

tip = tipSize [0][0]; //first element of the first array ("N/A") 
+0

當RadioButton傳遞一個值時,是@id值還是可以設置每個單選按鈕的值,然後「檢查」值?此外,是作爲字符串或Int傳遞的值?遇到麻煩時,我「int一個RadioButton」或將其轉換爲字符串。 – user2152750 2013-03-10 19:59:26

+0

@ user2152750 *當RadioButton傳遞一個值*請詳細說明。當它被點擊?您可以使用['OnCheckedChangedListener'](http://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.html)獲取RadioButton的Id,如果它是RadioGroup的一部分或使用' OnClickListener',如果它是單獨的。然後,你將不得不比較id來找出你想要的位置(例如'if(R.id.button = view.getId())String s = tipSize [0] [0];'另外,你應該閱讀如果你發現我的答案有幫助,你應該[通過點擊空白的選中標記來接受它] [http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html] – 2013-03-10 22:38:30

+0

@ user2152750所以它會變成綠色](http://meta.stackexchange.com/a/5235/202881)或者如果您需要進一步澄清(只要您閱讀我的回覆以及我發佈的鏈接),只需發表評論即可。 – 2013-03-16 00:27:00

0

一個快速和骯髒的方式是創建包含一個分隔的字符串的字符串組中的第二值一起,然後使用分離功能,其本身返回字符串數組的簡單陣列獲取個人價值。

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

會導致信f