如何将信息传递到Android Studio中的导航标题

时间:2019-06-16 11:30:34

标签: android navigation-drawer message-passing

我有一个向用户询问信息的活动,AskUserForDetailsActivity,还有另一个具有导航抽屉的TabActivity(紧随AskUser ..活动之后)。

我想将信息从AskUser ..发送到抽屉的导航头,该导航头又是另一种布局。

我尝试过:

 sendButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), TabActivity.class);
            intent.putExtra("Name", textView1.getText().toString());
            intent.putExtra("Age", textView2.getText().toString());
            intent.putExtra("Area", textView3.getText().toString());
            intent.putExtra("Occupation", textView4.getText().toString());
        }
    });

这来自AskForUser。 然后进入TabActivity:

 View v = getLayoutInflater().inflate(R.layout.nav_header_navigation,null);
    TextView receive1 = (TextView) v.findViewById(R.id.myName);
    receive1.setText(getIntent().getStringExtra("Name"));
    TextView receive2 = (TextView) v.findViewById(R.id.myAge);
    receive2.setText(getIntent().getStringExtra("Age"));
    TextView receive3 = (TextView) v.findViewById(R.id.myArea);
    receive3.setText(getIntent().getStringExtra("Area"));
    TextView receive4 = (TextView) v.findViewById(R.id.myOccupation);
    receive4.setText(getIntent().getStringExtra("Occupation"));

但是文本没有出现在我的导航标题中。我在做什么错了?

1 个答案:

答案 0 :(得分:0)

代替:

View v = getLayoutInflater().inflate(R.layout.nav_header_navigation,null);

您可以尝试下面的代码

NavigationView navigationView = findViewById(R.id.nav_view);
View v = navigationView.getHeaderView(0);