我有一个名为登录活动的课程。它记录用户并将用户带到UserAreaActivity.class。我使用intent从loginactivity获取用户的用户名和用户名到UserAreaActivity。
但问题是我有另一个名为Messages的类。我想从UserAreaActivity获取用户名到消息。
我尝试使用intent但是当我登录时,它直接转到了消息。跳过UserAreaActivity.class。
如何解决?
loginactivity.class
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final EditText etUsername = (EditText) findViewById(R.id.etUsername);
final EditText etPassword = (EditText) findViewById(R.id.etPassword);
final TextView tvRegisterLink = (TextView) findViewById(R.id.tvRegisterhere);
final Button bLogin = (Button) findViewById(R.id.bLogin);
tvRegisterLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class);
LoginActivity.this.startActivity(registerIntent);
}
});
bLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String username = etUsername.getText().toString();
final String password = etPassword.getText().toString();
// Response received from the server
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success) {
String name = jsonResponse.getString("name");
Intent intent = new Intent(LoginActivity.this, UserAreaActivity.class);
intent.putExtra("name", name);
intent.putExtra("username", username);
LoginActivity.this.startActivity(intent);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
builder.setMessage("Login Failed")
.setNegativeButton("Retry", null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
LoginRequest loginRequest = new LoginRequest(username, password, responseListener);
RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
queue.add(loginRequest);
}
});
}
}
UserAreaActivity.class
public class UserAreaActivity extends AppCompatActivity {
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_area);
final TextView etusername = (TextView) findViewById(R.id.textView2);
final TextView etwelcome = (TextView) findViewById(R.id.textView);
final ImageButton red = (ImageButton) findViewById(R.id.imageButton);
final ImageButton messages = (ImageButton) findViewById(R.id.imageButton2);
final ImageButton blue = (ImageButton) findViewById(R.id.imageButton3);
final ImageButton ping = (ImageButton) findViewById(R.id.imageButton4);
red.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent registerIntent = new Intent(UserAreaActivity.this, Report.class);
UserAreaActivity.this.startActivity(registerIntent);
}
});
messages.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent registerIntent = new Intent(UserAreaActivity.this, Messages.class);
UserAreaActivity.this.startActivity(registerIntent);
}
});
Intent intent = getIntent();
String name = intent.getStringExtra("name");
String username = intent.getStringExtra("username");
String message = "Welcome " + name;
etwelcome.setText(message);
etusername.setText(username);
}
messages.class
public class Messages extends AppCompatActivity {
private Button add_room;
private EditText room_name;
private ListView listView;
private ArrayAdapter<String> arrayAdapter;
private ArrayList<String> list_of_rooms = new ArrayList<>();
private String name;
private DatabaseReference root = FirebaseDatabase.getInstance().getReference().getRoot();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_messages);
add_room = (Button) findViewById(R.id.btn_add_room);
room_name = (EditText) findViewById(R.id.room_name_edittext);
listView = (ListView) findViewById(R.id.listView);
arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list_of_rooms);
listView.setAdapter(arrayAdapter);
add_room.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Map<String,Object> map = new HashMap<String, Object>();
map.put(room_name.getText().toString(),"");
root.updateChildren(map);
}
});
root.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Set<String> set = new HashSet<String>();
Iterator i = dataSnapshot.getChildren().iterator();
while (i.hasNext()){
set.add(((DataSnapshot)i.next()).getKey());
}
list_of_rooms.clear();
list_of_rooms.addAll(set);
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getApplicationContext(),Chat_Room.class);
intent.putExtra("room_name",((TextView)view).getText().toString() );
intent.putExtra("user_name", "HERE");
startActivity(intent);
}
});
}
}
答案 0 :(得分:1)
要解决此问题,您必须使用以下代码段来实施SharedPreferences
:
SharedPreferences preferences;
SharedPreferences.Editor editor;
private String TAG_USERNAME = "username";
在SharedPreferences
中设置用户名:
public void setUsername(String username) {
editor.putString(TAG_USERNAME,username);
editor.commit();
}
public String getUsername() {
return preferences.getString(TAG_USERNAME,"null");
}
希望它有所帮助。
答案 1 :(得分:0)
您可以使用 SharedPreferences
SharedPreferences类提供了一个通用框架,允许您保存和检索原始数据类型的持久键值对。您可以使用SharedPreferences保存任何原始数据:布尔值,浮点数,整数,长整数和字符串。这些数据将在用户会话中持续存在(即使您的应用程序被终止)。
用于在SharedPreferences中存储数据的示例代码
SharedPreferences preferences = getSharedPreferences("myPref", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("USER_NAME","NILESH");
editor.apply();
用于从SharedPreferences中检索数据的示例代码
SharedPreferences preferences = getSharedPreferences("myPref", MODE_PRIVATE);
String userName = preferences.getString("USER_NAME", "");