如何访问位于 Table 布局中的 TextView

How to access a TextView located in a Table Layout

我在"Activity A"中做了一个TableLayout 3x3(选择一个Table Layout,因为我认为它适合我的需要) 9 个单元格中有 7 个可点击并打开 "Activity B",这是我制作的自定义数字选择器。 我使用意图将数组和其他数据传递给 activiy B 我的数字选择器的想法是在我刚刚在 Activiy A 中单击的单元格中设置一个值(我想要类似 calendarDatePicker 的东西) 在 "Activity B" 中,我有一种方法可以在单击图像视图时更新 "Activity A" 和 finish() "Activity B" 的值,它工作得很好,除了它没有设置被单击的值文本视图

当 TextValue 包含在 Table 布局中时,如何从另一个 activity 设置 TextValue?

我不使用 intent,因为老实说,当 "Activity B" 关闭时,我不知道如何处理它。 我的意思是我应该在 "Activity A" 中的什么地方处理来自 "Activity B"

的这个意图

使用调试器我发现尝试使用 TextView Id 设置文本不起作用,因为它在调试器中显示 "null" 对象。

活动A

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

}

活动度 B

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

感谢 Pavan 的提示,我发现另外两个非常有用 post here and this one 正是我要找的东西。

像这样(确保你给 TableLayout 一个 id ):

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