基本上,当您单击注册菜单项时,会弹出一个警告对话框,单击注册后,将发送请求并重新创建活动(这是最好的方法,我可以在关闭警报对话框后刷新活动而不重新创建它?)。我有一个布尔值,我放入共享首选项,并据此,菜单项应更新,但它保持不变。我必须退出活动,下次我回到它,然后它会更新。但是,当我注销时,菜单会自动更新。我究竟做错了什么?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPreferenceAdapter = new PreferenceAdapter(this);
loginNo = mPreferenceAdapter.getLoginNo();
mPrefs = getSharedPreferences("mySharedPrefs", Context.MODE_PRIVATE);
isLoggedIn = mPrefs.getBoolean("isLoggedIn", false);
i = getIntent();
bundle = i.getBundleExtra("products");
parcelableProducts = bundle.getParcelable("parcelProducts");
parcelableShop = bundle.getParcelable("parcelShop");
bundle.putParcelable("parcelProductsFragment", parcelableProducts);
bundle.putParcelable("parcelProductsFragmentShop", parcelableShop);
shop = Parcels.unwrap(parcelableShop);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setUpViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
menuItemRegister = menu.findItem(R.id.menu_item_register);
if (loginNo == 0) {
menuItemRegister.setVisible(true);
} else {
changeLoginItem(isLoggedIn);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_peems:
i = new Intent(this, PeemActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
return true;
case R.id.menu_item_shops:
return true;
case R.id.menu_item_cart:
i = new Intent(this, CheckoutActivity.class);
startActivity(i);
return true;
case R.id.menu_item_login:
//loginDialog();
return true;
case R.id.menu_item_register:
if (isLoggedIn) {
logout();
} else {
registerDialog();
}
return true;
}
return super.onOptionsItemSelected(item);
}
public void changeLoginItem(boolean isLoggedIn) {
String logOut = getResources().getString(R.string.btnLogout);
String logIn = getResources().getString(R.string.btnRegister);
this.isLoggedIn = isLoggedIn;
menuItemRegister.setTitle(isLoggedIn ? logOut : logIn);
}
private void registerDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.register_dialog, null);
Button btnRegister = (Button) dialogView.findViewById(R.id.btnRegister);
etFirstName = (EditText) dialogView.findViewById(R.id.etFirstName);
etLastName = (EditText) dialogView.findViewById(R.id.etLastName);
etEmail = (EditText) dialogView.findViewById(R.id.etEmail);
etPassword = (EditText) dialogView.findViewById(R.id.etPassword);
builder.setView(dialogView);
final AlertDialog dialog = builder.show();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
registerInShop();
dialog.dismiss();
finish();
startActivity(getIntent());
invalidateOptionsMenu();
}
});
}
private void logout() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to logout?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mPrefs = getSharedPreferences("mySharedPrefs", Context.MODE_PRIVATE);
mEditor = mPrefs.edit();
mEditor.putBoolean("isLoggedIn", false);
mEditor.apply();
mPreferenceAdapter.removeLoginNo();
changeLoginItem(false);
finish();
startActivity(getIntent());
}
});
builder.setNegativeButton("NO", null);
AlertDialog dialog = builder.create();
dialog.show();
}
private void registerInShop() {
JSONObject shopInfoJSON = new JSONObject();
try {
shopInfoJSON.put("ShopNumber", String.valueOf(shop.getShopNo()));
shopInfoJSON.put("Server", String.valueOf(shop.getServer()));
shopInfoJSON.put("Db", String.valueOf(shop.getDb()));
shopInfoJSON.put("DbUser", String.valueOf(shop.getDbUser()));
shopInfoJSON.put("DbPass", String.valueOf(shop.getDbPass()));
} catch (JSONException e) {
e.printStackTrace();
}
JSONObject userInfoJSON = new JSONObject();
try {
userInfoJSON.put("FirstName", etFirstName.getText().toString());
userInfoJSON.put("LastName", etLastName.getText().toString());
userInfoJSON.put("Email", etEmail.getText().toString());
userInfoJSON.put("Password", etPassword.getText().toString());
userInfoJSON.put("Honorific", "Monsieur");
userInfoJSON.put("Company", "Company 1");
userInfoJSON.put("Company2", "company 2");
userInfoJSON.put("Address", "address");
userInfoJSON.put("Address2", "address2");
userInfoJSON.put("PostalCode", "1000");
userInfoJSON.put("City", "SomeCity");
userInfoJSON.put("Country", "SomeCountry");
userInfoJSON.put("Tel", "123123");
userInfoJSON.put("Mobile", "569852");
userInfoJSON.put("DOB", "1985-05-20");
} catch (JSONException e) {
e.getMessage();
}
JSONObject rootJSON = new JSONObject();
try {
rootJSON.put("ShopInfo", shopInfoJSON);
rootJSON.put("UserInfo", userInfoJSON);
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest registerInShopRequest = new JsonObjectRequest(Request.Method.POST, API.REGISTER_IN_SHOP, rootJSON, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, "Response Register: " + response);
int loginNo = 0;
try {
loginNo = response.getInt("LoginNo");
} catch (JSONException e) {
e.printStackTrace();
}
mPreferenceAdapter.saveLoginNo(loginNo);
mPrefs = getSharedPreferences("mySharedPrefs", Context.MODE_PRIVATE);
mEditor = mPrefs.edit();
mEditor.putBoolean("isLoggedIn", true);
mEditor.apply();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Response Error Register: " + error);
error.printStackTrace();
}
}) {
@Override
public Map<String, String> getHeaders() {
Map<String, String> headers = new HashMap<>();
headers.put(KEY_CONTENT_TYPE, "application/json; charset=utf-8");
headers.put(KEY_X_PROVIDER, "some");
headers.put(KEY_AUTHORIZATION, "");
return headers;
}
};
ApplicationController.getInstance().addToRequestQueue(registerInShopRequest);
}
private void setUpViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(WishlistFragment.newInstance(), "Wishlist");
adapter.addFragment(HistoryFragment.newInstance(), "History");
adapter.addFragment(OffersFragment.newInstance(bundle), "Offers");
adapter.addFragment(ProductsFragment.newInstance(bundle), "Products");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}