2017-05-28 48 views
0

我在「活動A」中選擇了一個TableLayout 3x3(選擇表格佈局,因爲我認爲它適合我的需要) 9個單元格中的7個是可點擊並打開的「活動B 「這是我做的一個自定義號碼選擇器。 我用意圖傳遞一個數組和其他數據來激活B 我的數字選擇器的想法是在單元格中設置一個值,我在Activityiy A中單擊(我想要的東西就像calendarDatePicker) 在「Activity B」我有一個方法來更新從「活動A」的值,並完成()「活動B」時,單擊圖像視圖很好的作品非常好,但它不會設置單擊文本視圖的值如何訪問位於表格佈局中的TextView

如何可以當TextValue包含在表格佈局中時,我從另一個活動設置TextValue?

我不使用意圖,因爲老實說,當「活動B」關閉時我不知道如何處理它。 我的意思是在「活動A」中我應該從「活動B」處理這個意圖

使用調試器我發現嘗試使用TextView設置文本不起作用,因爲它顯示「null」對象在調試器中。

Activiy一個

package com.example.racu.threebythree; 


public class threebythree extends AppCompatActivity { 

    public static ArrayList<Integer> ColumnA = new ArrayList<Integer>(); 
    public static ArrayList<Integer> ColumnB = new ArrayList<Integer>(); 
    public static ArrayList<Integer> ColumnC = new ArrayList<Integer>(); 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_three_by_three); 

     for (int i = 1; i < 28; i++) { 
      if (i > 0 && i < 10) 
       ColumnA.add(i); 
      if (i > 10 && i < 19) 
       ColumnB.add(i); 
      if (i > 19 && i < 28) 
       ColumnC.add(i); 
     } 

    } 

    public void openNumberPicker(View v) { 

//  I used this to "guess" the location of the cell in the table layout as I could not find a way to do it. 

     String cellName = v.getResources().getResourceName(v.getId()); 
     String cellLetter = cellName.substring(cellName.lastIndexOf("/") + 1); 

//  Send intent 

     Intent intent = new Intent(this, NumberPicker.class); 
     intent.putExtra("Id", (cellLetter.substring(0, 1) + cellLetter.substring(2)).toUpperCase()); 

     switch (cellLetter.substring(0, 1)) { 
      case ("a"): 
       intent.putIntegerArrayListExtra("Column", ColumnA); 
       break; 
      case ("b"): 
       intent.putIntegerArrayListExtra("Column", ColumnB); 
       break; 
      case ("c"): 
       intent.putIntegerArrayListExtra("Column", ColumnC); 
       break; 
     } 
     intent.putExtra("Cell ID", v.getId()); 

     startActivity(intent); 
    } 

} 

活性起着乙

package com.example.racu.threebythree; 

public class NumberPicker extends AppCompatActivity { 

    GridView numberPicker; 
    ArrayList<Integer> numbersToDisplay; 
    TextView viewToDisplay; 

    ImageView ok, cancel; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_number_picker); 
     numberPicker = (GridView) findViewById(R.id.arrayNumbers); 
     viewToDisplay = (TextView) findViewById(R.id.number_to_select); 
     ok = (ImageView) findViewById(R.id.add_number_to_card); 
     ok.setClickable(false); 

     Intent intent = getIntent(); 
     String idFromIntent = intent.getStringExtra("Id"); 
     numbersToDisplay = intent.getIntegerArrayListExtra("Letter"); 
     TextView numberPosition = (TextView) findViewById(R.id.numberPosition); 
     numberPosition.setText(idFromIntent); 
     final TextView chosenNumber = (TextView) findViewById(R.id.chosenNumber); 

     ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this, R.layout.number_for_picker, numbersToDisplay); 
     numberPicker.setAdapter(adapter); 

     numberPicker.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

      public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 

       chosenNumber.setText(((TextView) v).getText()); 
       ok.setImageResource(R.drawable.ok_blue); 
       ok.setClickable(true); 
      } 
     }); 

    } 

    public void updateThreByThree(View v) { 

     Intent intent = getIntent(); 
     int currentCell = intent.getIntExtra("Cell ID", 0); 
     String idFromIntent = intent.getStringExtra("Id").substring(0, 1); 

     TextView chosenNumber = (TextView) findViewById(R.id.chosenNumber); 

// Here I try to Set the text to the cell in Activity A, using its R.id, but in the debugger I get a null reference, therefore my app crashes 
     TextView currentCellToEdit = (TextView) findViewById(currentCell); 
     currentCellToEdit.setText(chosenNumber.getText()); 

     int temp = Integer.parseInt(chosenNumber.getText().toString()); 


     switch (idFromIntent) { 
      case "A": 
       threebythree.ColumnA.remove(Integer.valueOf(temp)); 
       break; 
      case "B": 
       threebythree.ColumnB.remove(Integer.valueOf(temp)); 
       break; 
      case "C": 
       threebythree.ColumnC.remove(Integer.valueOf(temp)); 
       break; 
     } 
     finish(); 
    } 

    public void cancel(View view) { 

     finish(); 
    } 
} 

感謝帕文爲我找到了兩個非常有幫助後here的提示,這one正是我一直在尋找用的。

+0

沒有能夠得到這個問題可以是U希望從其他活動,結果https://developer.android獲取數據。 com/training/basics/intents/result.html – Pavan

+1

謝謝@Pavan,這正是我一直在尋找的,雖然你的鏈接有點混亂,對我而言,發現你的提示非常有幫助。 感謝您的提示,在StackOverFlow中發現了更多有用的鏈接。 [https://stackoverflow.com/questions/10407159/how-to-manage-startactivityforresult-on-android?answertab=votes#tab-top] – Racu

回答

1

就像那個(確保你給TableLayout一個id):

TableLayout tl = (TableLayout)findViewById(R.id.tableLayout); 
TextView tv = (TextView)tl.findViewById(R.id.textView); 
+0

試過但沒有奏效,我無法參考Texview ,我相信這是因爲我試圖從不同的活動中調用它,而不是它自己的。泰 – Racu