如何从Parse中检索用户名

时间:2016-12-10 20:27:18

标签: java android parse-platform back4app

我尝试将当前用户名添加到我的应用中滑动菜单的开头。

-similar to this image I got from the internet-

我尝试添加此代码,但它给了我一个错误:

ParseUser.getCurrentUser();
t_username = (TextView) findViewById(R.id.username);
t_username.setText(user.getUsername());

这是滑动菜单代码:

package com.androidbegin.parselogintutorial;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.parse.ParseUser;
import static com.androidbegin.parselogintutorial.R.drawable.user;
import static com.androidbegin.parselogintutorial.R.id.nav_camera;

public class SlidingMenu extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {


    ImageButton lectureNotes, labNotes, quizzes, mt;
    TextView t_username;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sliging_menu);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        ParseUser.getCurrentUser();
        t_username = (TextView) findViewById(R.id.username);
        ParseUser.getCurrentUser();
        t_username.setText(user.getUsername());

        final ProgressBar simpleProgressBar = (ProgressBar) findViewById(R.id.simpleProgressBar);
        simpleProgressBar.setVisibility(View.INVISIBLE);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

        .
        .
        .

        navigationView.setNavigationItemSelectedListener(this);

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.sliging_menu, menu);
        return true;


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

        .
        .
        .

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;

    }


}

这是导航标题的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:background="@drawable/side_nav_bar"
    android:gravity="bottom"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">


    <ImageView
        android:layout_width="30dp"
        android:layout_height="60dp"
        android:layout_marginLeft="3dp"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        app:srcCompat="@drawable/user"
        android:scaleType="fitEnd"
        android:id="@+id/imageView3" />

    <TextView
        android:layout_width="match_parent"
        android:id="@+id/username"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginBottom="10dp"
        android:textColor="#fff"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        android:layout_alignBottom="@+id/imageView3"
        android:layout_toRightOf="@+id/imageView3"
        android:layout_toEndOf="@+id/imageView3" />


</RelativeLayout>

从Parse中检索数据的正确代码是什么?

0 个答案:

没有答案