导航抽屉中的用户帐户
问题描述:
我已成功设置使用新设计支持库的导航抽屉,但不知道如何进一步处理。 我需要提供用户帐户,有人可以提供一个代码或如何通过这个想法。导航抽屉中的用户帐户
这是代码我现在有。
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<RelativeLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />
</RelativeLayout>
<android.support.design.widget.NavigationView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/header"
app:menu="@menu/nav_menu" />
答
假设你已经设置了SQLite的辅助类(example),和已有的知识来获得使用HashMap的用户详细信息,您可以轻松拥有通过添加一对夫妇的意见您的导航这个用做抽屉xml。
您将需要添加一个textview和一个circleimageview from hdondenhof,它将显示您的用户名和个人资料图片。如果你想要显示更多的视图,你也可以这样做。为它们分配ID,以便您可以在其对应的Java对话框中显示所需的信息。
在您的导航抽屉片段中,如果已经存储在您的SQLite数据库中,您可以在这里传递用户名和图片参数。在我的情况下,我通常会在我的片段的onCreateView中调用它:
// Initialize and declare SQLite handler class to retrieve user's details:
final SQLiteHandler sqLiteHandler = new SQLiteHandler(getActivity());
final HashMap<String, String> user = sqLiteHandler.getUserDetails();
// Retrieving the user's username for display on navUsername
String setUsername = user.get("username");
navUsername = (TextView) view.findViewById(R.id.nav_username);
navUsername.setText("Welcome, " + setUsername);
而这的确很简单。 circleimageview的工作原理与imageview相似,因此您可以从数据库获取用户的图片,或者如果未找到图片,则显示可绘制的图片。