2017-04-20 5 views
0

別のレイアウト内のIDを参照するMainActivityからナビゲーション・ドロワー内のユーザー名と電子メールのテキストを設定する方法を探しています。以下はMainActivityからのレイアウトを使用してナビゲーション・ドロワーにログインしたユーザーを表示

nav_header_main.xml 

MainActivity Screenshot 1

MainActivity Screenshot 2

コードは、私は "R.id." が必要しかしactivity_main.xmlからTextViewsを参照するユーザーにはログインして表示されますnav_header_main.xml内のユーザ名と電子メールTextViewsを参照して、ユーザの詳細をナビゲーションドロワヘッダに表示できるようにします。

MainActivity.java

if(!SharedPrefManager.getInstance(this).isLoggedIn()){ 

      finish(); 
      startActivity(new Intent(this, LoginActivity.class)); 
     } 
     textviewUsername = (TextView)findViewById(R.id.username); 
     textviewEmail = (TextView)findViewById(R.id.email); 
    /* 
     textviewUsername = (TextView)findViewById(R.id.textViewUsernameNav); 
    textViewEmail= (TextView)findViewById(R.id.textViewEmailNav); */ 

     textviewEmail.setText(SharedPrefManager.getInstance(this).getEmail()); 
     textviewUsername.setText(SharedPrefManager.getInstance(this).getUsername()); 

nav_header_xml

android:id="@+id/nav_header_main_id" 

<TextView 
    android:id="@+id/textViewUsernameNav" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingTop="@dimen/nav_header_vertical_spacing" 
    android:text="Username" 
    android:textAppearance="@style/TextAppearance.AppCompat.Body1" /> 

<TextView 
    android:id="@+id/textViewEmailNav" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="[email protected]" /> 
+0

追加ナビゲーションビューのレイアウトと主な活動のXML –

+0

をナビゲーションheaderviewのユーザー名と電子メールのTextView – FAT

+0

問題を更新するために私の答えをお試しください私は、nav_header_main.xml内のTextViewを参照することを許可していません –

答えて

2
  1. NavigationView.getHeaderView(0)を使用してヘッダー表示を取得します。
  2. HeaderView.findViewById()を使用して、子ビューの参照を取得します。

これを試してみてください:

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

    NavigationView mNavigationView; 
    View mHeaderView; 

    TextView textViewUsername; 
    TextView textViewEmail; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     ................... 
     .......................... 

     // NavigationView 
     mNavigationView = (NavigationView) findViewById(R.id.nav_view); 

     // NavigationView Header 
     mHeaderView = mNavigationView.getHeaderView(0); 

     // View 
     textViewUsername = (TextView) mHeaderView.findViewById(R.id.textViewUsernameNav); 
     textViewEmail= (TextView) mHeaderView.findViewById(R.id.textViewEmailNav); 

     // Set username & email 
     textViewUsername.setText(SharedPrefManager.getInstance(this).getUsername()); 
     textViewEmail.setText(SharedPrefManager.getInstance(this).getEmail()); 


     mNavigationView.setNavigationItemSelectedListener(this); 
    } 
} 

希望これが役立ちます〜

+0

ありがとうございます!出来た! :D –

+0

大歓迎:) – FAT

0

、ヘッダビューに次のようなものfindViewByIdを使用してみてください:

NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view); 
View headerView = navigationView.getHeaderView(0); 
textviewUsername= (TextView) headerView.findViewById((R.id.username)); 
関連する問題