【问题标题】:How to access a TextView located in a Table Layout如何访问位于表格布局中的 TextView
【发布时间】:2017-05-28 19:35:14
【问题描述】:

我在“活动 A”中制作了一个 3x3 的 TableLayout(选择了一个表格布局,因为我认为它适合我的需要) 9 个单元格中有 7 个可单击并打开“活动 B”,这是我制作的自定义数字选择器。 我使用意图将数组和其他数据传递给活动 B 我的数字选择器的想法是在我刚刚在 Activiy A 中单击的单元格中设置一个值(我想要类似于 calendarDatePicker 的东西) 在“活动 B”中,我有一种方法可以在单击图像视图时更新“活动 A”和完成()“活动 B”的值,除了它没有为单击的文本视图设置值之外,它工作得非常好

当 TextValue 包含在表格布局中时,如何从另一个活动设置 TextValue?

我不使用意图,因为老实说,当“活动 B”关闭时,我不知道如何处理它。 我的意思是我应该在“活动 A”的哪个位置处理来自“活动 B”的意图

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

活动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 的提示,我发现了另外两个非常有用的帖子 here,而这个 one 正是我想要的。

【问题讨论】:

标签: android android-intent textview tablelayout


【解决方案1】:

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

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

【讨论】:

  • 试过但没用,我无法引用 Texview,我相信这是因为我试图从不同的活动中调用它,而不是它自己的。泰
猜你喜欢
  • 2012-07-13
  • 1970-01-01
  • 2014-06-02
  • 1970-01-01
  • 2013-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多