2016-08-28 35 views
0

HomeActivity.java(導航活動)如何在導航活動的導航抽屜中寫入用戶名從其片段之一?

import android.app.Fragment; 
    import android.app.FragmentManager; 
    mport android.app.FragmentTransaction; 
    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.TextView; 

    import com.example.dellpc.reuz_app.DTO.User; 
    import com.example.dellpc.reuz_app.Fragments.BrowseAdFragment; 
    import com.example.dellpc.reuz_app.Fragments.LoginFragment; 
    import com.example.dellpc.reuz_app.Fragments.NetworkErrorFragment; 
    import com.example.dellpc.reuz_app.Fragments.TopMainFragment; 
    import com.example.dellpc.reuz_app.R; 
    import com.example.dellpc.reuz_app.utils.CheckNetworkConnection; 
    import com.example.dellpc.reuz_app.utils.NetworkErrorFragmentSetter; 

    public class HomeActivity extends AppCompatActivity 
    implements NavigationView.OnNavigationItemSelectedListener { 
    private Fragment fragment; 
    private String title; 
    private DrawerLayout drawer; 
    private NavigationView navigationView; 
    private View layout; 
    private User user=null; 
     @Override 

    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_home); 
    Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    getSupportActionBar().setHomeButtonEnabled(true); 
    getSupportActionBar().setTitle("REUZ"); 
    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) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 
    navigationView.bringToFront(); 
    System.out.println(getSupportActionBar().getHeight()); 
    View headerLayout = navigationView.getHeaderView(0); 
    layout=headerLayout.findViewById(R.id.navigationHeader); 
    getSupportActionBar().setTitle("REUZ"); 

    this.setInitialFragment(); 
    this.listeners(); 

    /*this.getUserObject();*/ 
} 
    public void listeners(){ 
    layout.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
    if(CheckNetworkConnection.isConnectionAvailable(HomeActivity.this)) 
     { 
      getLoginFragment(); 
     } 
     else{ 
     fragment=       NetworkErrorFragmentSetter.getNetworkErrorFragment(HomeActivity.this); 
     replaceFragment(); 

    } 
    } 
}); 

     getFragmentManager().addOnBackStackChangedListener(new    FragmentManager.OnBackStackChangedListener() { 
    @Override 
    public void onBackStackChanged() { 
     Fragment currentFragment = getFragmentManager().findFragmentById(R.id.content_frame); 
     if (currentFragment instanceof TopMainFragment) { 
      getSupportActionBar().setTitle("REUZ"); 
     } 
     else if (currentFragment instanceof BrowseAdFragment) { 
      getSupportActionBar().setTitle("All Categories"); 
     } 
     else if (currentFragment instanceof LoginFragment) { 
      getSupportActionBar().setTitle("LOGIN"); 
     } 
     else if(currentFragment instanceof NetworkErrorFragment){ 
      getSupportActionBar().setTitle("NETWORK ERROR"); 
     } 

       } 
}); 
    } 
@Override 
    public void onBackPressed() { 
    //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.home, 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); 
} 
     @SuppressWarnings("StatementWithEmptyBody") 
     @Override 
     public boolean onNavigationItemSelected(MenuItem item) { 
    // Handle navigation view item clicks here. 

    int id = item.getItemId(); 

    if (id == R.id.nav_home) { 
     // Handle the home action 
     setInitialFragment(); 


    } else if (id == R.id.nav_browse) { 
       fragment=new BrowseAdFragment(); 

    } else if (id == R.id.nav_submitAd) { 
       Intent intent=new Intent(getApplicationContext(),SubmitAdActivity.class); 
       startActivity(intent); 
    } else if (id == R.id.nav_help) { 

    } else if (id == R.id.nav_my_ads) { 

    } else if (id == R.id.nav_wishList) { 

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

public void setTitleOnActionBar(String title) { 


    getSupportActionBar().setTitle(title); 
} 


public void setInitialFragment(){ 
    fragment=new TopMainFragment(); 
    FragmentTransaction ft=getFragmentManager().beginTransaction(); 
    ft.replace(R.id.content_frame, fragment); 
    // ft.addToBackStack(null); 
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
    ft.commit(); 

    drawer.closeDrawer(GravityCompat.START); 
} 
public void getLoginFragment(){ 
    fragment=new LoginFragment(); 
    FragmentTransaction ft=getFragmentManager().beginTransaction(); 
    ft.replace(R.id.content_frame, fragment); 
    ft.addToBackStack(null); 
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
    ft.commit(); 

    drawer.closeDrawer(GravityCompat.START); 

} 
public void replaceFragment(){ 
    FragmentTransaction ft=getFragmentManager().beginTransaction(); 
    ft.replace(R.id.content_frame, fragment); 
    ft.addToBackStack(null); 
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
    ft.commit(); 

    drawer.closeDrawer(GravityCompat.START); 
} 
@Override 
public void onResume() { 
    super.onResume(); 
    // Set the title 

} 
public void getUserObject(User user){ 
    this.user=user; 

    if(user!=null){ 
     changeLayoutName(); 
    } 
} 
public void changeLayoutName(){ 
    TextView name= (TextView)layout.findViewById(R.id.textView8); 
    name.setText(user.getName()); 
} 
} 

LoginFragment.java(片段)

  import android.app.Fragment; 
     import android.content.Intent; 
     import android.os.Bundle; 
     import android.support.design.widget.NavigationView; 
     import android.support.v7.widget.Toolbar; 
     import android.text.Editable; 
     import android.text.TextWatcher; 
     import android.view.LayoutInflater; 
     import android.view.View; 
     import android.view.ViewGroup; 
     import android.widget.Button; 
     import android.widget.EditText; 
     import android.widget.TextView; 
     import android.widget.Toast; 

     import com.example.dellpc.reuz_app.Activities.HomeActivity; 
     import com.example.dellpc.reuz_app.Activities.RegisterActivity; 
     import com.example.dellpc.reuz_app.Activities.UnableToLoginActivity; 
     import com.example.dellpc.reuz_app.DTO.User; 
     import com.example.dellpc.reuz_app.R; 
     import com.example.dellpc.reuz_app.Validator.IValidation; 
     import com.example.dellpc.reuz_app.Validator.Validation; 
     import com.example.dellpc.reuz_app.dao.IUserDAO; 
     import com.example.dellpc.reuz_app.dao.UserDAO; 


     public class LoginFragment extends Fragment { 

    private EditText emailET, passwordET; 
    private Button loginBTN; 
    private TextView utlTV, registerTV, invalidEmailTV, invalidPasswordTV; 
    private View view; 
    private NavigationView navigationView; 
    private User user; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // keep the fragment and all its data across screen rotation 
     setRetainInstance(true); 
    } 



    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     view = inflater.inflate(R.layout.fragment_login, container, false); 
     Toolbar toolbar = (Toolbar)view.findViewById(R.id.toolbar); 

     ((HomeActivity)getActivity()).getSupportActionBar().setTitle("LOGIN"); 
     this.mapping(); 
     this.listeners(view); 
     return view; 
    } 


    public void mapping(){ 
     emailET = (EditText)view.findViewById(R.id.emailET); 
     passwordET = (EditText)view.findViewById(R.id.passwordET); 
     loginBTN = (Button)view.findViewById(R.id.loginBTN); 
     utlTV = (TextView)view.findViewById(R.id.utlTV); 
     invalidEmailTV = (TextView)view.findViewById(R.id.invalidEmailTV); 
     invalidPasswordTV = (TextView)view.findViewById(R.id.invalidPasswordTV); 
     registerTV = (TextView)view.findViewById(R.id.registerTV); 
    } 

    public void listeners(View view){ 
     loginBTN.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       String email = emailET.getText().toString(); 
       String password = passwordET.getText().toString(); 
       validateAndMoveToActivity(email, password); 

      } 
     }); 

     utlTV.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent utlIntent = new Intent(getActivity(), UnableToLoginActivity.class); 
       startActivity(utlIntent); 
      } 
     }); 

     registerTV.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent registerIntent = new Intent(getActivity(), RegisterActivity.class); 
       startActivity(registerIntent); 
      } 
     }); 

     emailET.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

      } 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 

      } 

      @Override 
      public void afterTextChanged(Editable s) { 
       invalidEmailTV.setText(""); 
       invalidPasswordTV.setText(""); 
      } 
     }); 

     passwordET.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

      } 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 

      } 

      @Override 
      public void afterTextChanged(Editable s) 
      { 
       invalidEmailTV.setText(""); 
       invalidPasswordTV.setText(""); 
      } 
     }); 
    } 

    public void validateAndMoveToActivity(String email, String password){ 
     IValidation iValidation = new Validation(); 
     if(!iValidation.isFilled(email) && iValidation.isFilled(password)){ 
      invalidEmailTV.setText("This field is required"); 
     } 
     else if(iValidation.isFilled(email) && !iValidation.isFilled(password)) { 
      invalidPasswordTV.setText("This field is required"); 
     } 
     else if(!iValidation.isFilled(email) && !iValidation.isFilled(password)){ 
      invalidEmailTV.setText("This field is required"); 
      invalidPasswordTV.setText("This field is required"); 
     } 
     else 
     { 
      IUserDAO iUserDAO = new UserDAO(getActivity().getApplicationContext()); 

      /* 
       if(iUserDAO.isEmailPasswordExist(email, password))*/ 
      if(iUserDAO.isEmailExist(email)){ 
      user=iUserDAO.isEmailPasswordExist(email,password); 
      if(user!=null) 
      { 
        Toast.makeText(view.getContext(), "Successful Login", Toast.LENGTH_SHORT).show(); 
       HomeActivity home=new HomeActivity(); 
        home.getUserObject(user); 
        /* Intent homeACIntent = new Intent(view.getContext(), HomeActivity.class); 
        homeACIntent.putExtra("user",user); 
        startActivity(homeACIntent);*/ 
        // Remove this fragment. 
        getActivity().getFragmentManager().popBackStack(); 
       } 
       else { 
        invalidPasswordTV.setText("Invalid email or password"); 
       } 
      } 
      else { 
       // After successful login where do you want to move your activity. 
       invalidEmailTV.setText("Invalid phone number or email"); 
      } 
     } 
    } 
@Override 
public void onSaveInstanceState(Bundle state) { 
    super.onSaveInstanceState(state); 
    state.putSerializable("user",user); 
} 


} 

HomeActivity包含兩個Fragments-登錄和頂級 最初,它顯示了頂部片段 登錄片段有一個選項註冊 成功註冊後,用戶導航到登錄片段 現在登錄後,控件必須到達「頂部」,並且用戶的名稱必須寫入導航D rawer 怎麼辦?

這是我的抽屜.T想寫用戶的歡迎,而不是名稱來Reuz

HomeActivity

回答

0

我看到你節省您的本地數據庫的用戶詳細信息,然後試圖加載用戶成功登錄後的數據。您面臨的問題是TextView未更新。一種可能的解決方案,您可以從TopMainFragment本身的onViewCreated()方法調用changeLayoutName()方法。

+0

感謝我得到它,現在它的工作.... – Johny