您可以使用 Intent 从 SharedPreferences 将数据传递给 ListView
公共类 MainActivity 扩展 AppCompatActivity 实现 View.OnClickListener {
private EditText nameEditText, rollEditText, deptEditText, semesterEditText;
private Button submittButton;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameEditText = findViewById(R.id.nameEditTextID);
rollEditText = findViewById(R.id.rollEditTextID);
deptEditText = findViewById(R.id.deptEditTextID);
semesterEditText = findViewById(R.id.semesterEditTextID);
submittButton = findViewById(R.id.submitButtonID);
textView = findViewById(R.id.textViewID);
submittButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.submitButtonID) {
String userName = nameEditText.getText().toString();
String userRoll = rollEditText.getText().toString();
String userDept = deptEditText.getText().toString();
String userSemester = semesterEditText.getText().toString();
if (userName.equals("") && userRoll.equals("") && userDept.equals("") && userSemester.equals("")) {
Toast.makeText(getApplicationContext(), "Please enter data", Toast.LENGTH_SHORT).show();
} else {
SharedPreferences sharedPreferences = getSharedPreferences("Details", 0);
//SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("userNameKey", userName);
editor.putString("userRollKey", userRoll);
editor.putString("userDeptKey", userDept);
editor.putString("userSemesterKey", userSemester);
editor.apply();
nameEditText.setText("");
rollEditText.setText("");
deptEditText.setText("");
semesterEditText.setText("");
Toast.makeText(getApplicationContext(), "Saved successfully", Toast.LENGTH_SHORT).show();
if (sharedPreferences.contains("userNameKey") && sharedPreferences.contains("userRollKey") && sharedPreferences.contains("userDeptKey")
&& sharedPreferences.contains("userSemesterKey")) {
Intent intent = new Intent(MainActivity.this, ListDataActivity.class);
startActivity(intent);
}
/*if (sharedPreferences.contains("userNameKey") && sharedPreferences.contains("userRollKey") && sharedPreferences.contains("userDeptKey")
&& sharedPreferences.contains("userSemesterKey")) {
String name = sharedPreferences.getString("userNameKey", "Data not found");
String roll = sharedPreferences.getString("userRollKey", "Data not found");
String department = sharedPreferences.getString("userDeptKey", "Data not found");
String semester = sharedPreferences.getString("userSemesterKey", "Data not found");
Toast.makeText(getApplicationContext(), "Loaded successfully", Toast.LENGTH_SHORT).show();
textView.setText(name + "\n" + roll + "\n" + department + "\n" + semester);
}*/
}
}
}
公共类 ListDataActivity 扩展 AppCompatActivity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_data);
listView = findViewById(R.id.listViewID);
SharedPreferences sharedPreferences = getSharedPreferences("Details", 0);
String name = sharedPreferences.getString("userNameKey", "Data not found");
String roll = sharedPreferences.getString("userRollKey", "Data not found");
String department = sharedPreferences.getString("userDeptKey", "Data not found");
String semester = sharedPreferences.getString("userSemesterKey", "Data not found");
Toast.makeText(this, "Roll is:" + roll, Toast.LENGTH_SHORT).show();
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add(name);
arrayList.add(roll);
arrayList.add(department);
arrayList.add(semester);
ArrayAdapter<String>arrayAdapter = new ArrayAdapter<String>(this,R.layout.list_item,R.id.sampleTextViewID,arrayList);
listView.setAdapter(arrayAdapter);
}
}