导航抽屉上显示的名称和电子邮件的更改名称类似于Playstore的配置文件

时间:2018-08-04 13:33:03

标签: android navigation-drawer

我想将导航抽屉名称和电子邮件中的配置文件外观更改为Playstore中的名称和电子邮件用户。 这是我使用MySQL登录的代码:

LoginActivity.java

 @Override
                public void onResponse(String response) {
                    try {
                        JSONObject jsonObject= new JSONObject(response);
                        String success = jsonObject.getString("success");
                        JSONArray jsonArray = jsonObject.getJSONArray("login");

                        if (success.equals("1")){

                            for (int i=0; i<jsonArray.length();i++){
                                JSONObject object= jsonArray.getJSONObject(i);
                                String name = object.getString("name").trim();
                                Toast.makeText(LoginActivity.this, "Login Berhasil. \nSelamat Datang : "+name, Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                                intent.putExtra("name", name);
                                intent.putExtra("email", email);
                                startActivity(intent);
                                loading.setVisibility(View.GONE);

                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        loading.setVisibility(View.GONE);
                        btnLogin.setVisibility(View.VISIBLE);
                        Toast.makeText(LoginActivity.this, "Error Check Kembali Email dan Password"+e.toString(), Toast.LENGTH_SHORT).show();
                    }
                }
            },
            new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            loading.setVisibility(View.GONE);
            btnLogin.setVisibility(View.VISIBLE);
            Toast.makeText(LoginActivity.this, "Error"+error.toString(), Toast.LENGTH_SHORT).show();
        }

从登录名到显示在导航栏上的名称和电子邮件的个人资料,我的问题就显而易见了。

这是我的导航来源

Nav_drawer.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:background="@color/colorAccent"
    android:gravity="bottom"
    android:padding="16dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:layout_height="match_parent"
    android:id="@+id/nav">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@mipmap/ic_launcher_round"/>

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="8dp"
        android:text="Your Name"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        />
    <TextView
        android:id="@+id/email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Your Email@gmail.com"/>
</LinearLayout>

home.java

public class MainActivity extends AppCompatActivity {
    private DrawerLayout drawer;
    private TextView name,email;

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

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        name = findViewById(R.id.name);
        email = findViewById(R.id.email);

        Intent intent = getIntent();
        String extraName = intent.getStringExtra("name");
        String extraEmail = intent.getStringExtra("email");

        name.setText(extraName);
        email.setText(extraEmail);

        drawer = findViewById(R.id.drawer_layout);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawer,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
    }

    @Override
    public void onBackPressed() {
        if (drawer.isDrawerOpen(GravityCompat.START)){
            drawer.closeDrawer(GravityCompat.START);
        }else {
            super.onBackPressed();
        }

    }
}

1 个答案:

答案 0 :(得分:0)

您必须先获取标题视图,然后再获取您感兴趣的子视图,然后才能设置文本,例如

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //... other onCreate setup        

        NavigationView mNavigationView = findViewById(R.id.nav_view);               
        View headerView = mNavigationView.getHeaderView(0);
        // get user name and email textViews
        TextView userName = headerView.findViewById(R.id.text_user_name);
        TextView userEmail = headerView.findViewById(R.id.text_email_address);
        // set user name and email
        userName.setText("username");
        userEmail.setText("email.user@domain.com");

}