我的应用程序中,我想在用户登录后使用其昵称和电子邮件更新导航抽屉。
从我的MainActivity
中,我使用startActivityForResult(intent, PICK_ACCOUNT_REQUEST);
方法启动一个LoginActivity
,以获取已注册或已登录的用户。
在LoginActivity
返回Intent data
结果(他的NAME
和EMAIL
)回到MainActivity
之后,将调用onActivityResult()
方法,并在那里尝试使用新接收到的数据更新类的全局变量NAME
和EMAIL
,但是没有成功:在每次注册或登录后,这两个字段都不会显示在导航抽屉中。以下是我的代码:
在MainActivity
类的开头,我分配了两个变量:
String NAME = "Sign in", EMAIL = "";
这两个值被插入到我的导航抽屉中。在我从 LoginActivity
接收到结果后,我想要在 onActivityResult()
方法中更新这两个字段。如下所示:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
// Check which request we're responding to
if (requestCode == PICK_ACCOUNT_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// The user picked a contact.
String usr = data.getStringExtra("username");
String mal = data.getStringExtra("email");
this.NAME = usr;this.EMAIL=mal;
}
}
}
尽管完成 LoginActivity
后,我仍然无法成功地从 MainActivity
访问包含用户名和密码的2个EditText,所以导航抽屉中显示了旧值。事实上,这2个EditText是在RecyclerView.ViewHolder
内管理的。
我该如何设置观察者或类似的机制来动态更新我的导航抽屉的 RecyclerView.Adapter
?
更多详细信息如下:
*编辑*
PS: 我的导航抽屉是使用 RecyclerView 实现的,因此适配器中有一个 RecyclerView.Adapter
类和一个在适配器中的 RecyclerView.ViewHolder
类。在MainActivity
中,我有以下代码:
public class MainActivity extends ActionBarActivity{
ArrayList<DrawerItem> dataList;
RecyclerView mRecyclerView; // Declaring RecyclerView
RecyclerView.Adapter mAdapter; // Declaring Adapter For Recycler View
RecyclerView.LayoutManager mLayoutManager; // Declaring Layout Manager as a linear layout manager
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mTitle, mDrawerTitle;
Toolbar toolbar;
String NAME, EMAIL; int AVATARresID; //TODO update Navigation Drawer fields
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* Assigning the toolbar object to the view
and setting the the Action bar to our toolbar */
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
// enable ActionBar app icon to behave as action to toggle nav drawer
getSupportActionBar().setDisplayHomeAsUpEnabled(true); //pulsante drawer
getSupportActionBar().setHomeButtonEnabled(true); //pulsante dietro
//Initializing
mTitle = mDrawerTitle = getTitle();
AVATARresID = R.mipmap.aka;
// Add Drawer Item to dataList
dataList = new ArrayList<>();
dataList.add(new DrawerItem(NAME,EMAIL,AVATARresID));
dataList.add(new DrawerItem("Account", R.mipmap.ic_account));
dataList.add(new DrawerItem("Tuoi Tour", R.mipmap.ic_events));
dataList.add(new DrawerItem("Prenota Tour", R.mipmap.ic_action_search));
// dataList.add(new DrawerItem("My Favorites")); // adding a header to the list
dataList.add(new DrawerItem("Others")); // adding a header to the list
dataList.add(new DrawerItem("Contatti", R.mipmap.ic_about));
dataList.add(new DrawerItem("Tutorial", R.mipmap.ic_help));
mRecyclerView = (RecyclerView) findViewById(R.id.RecyclerView);
mRecyclerView.setHasFixedSize(true);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mAdapter = new MyAdapter(dataList, mSelectedPositions);
mRecyclerView.setAdapter(mAdapter);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout,toolbar,
R.string.drawer_open, R.string.drawer_close){
@Override
public void onDrawerOpened(View drawerView) {...}
@Override
public void onDrawerClosed(View drawerView) {...}
};
// Drawer Listener set to the Drawer toggle
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
mRecyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(this, mRecyclerView, new RecyclerItemClickListener.OnItemClickListener() {...});
}
}