我正在开发一个用户在MainActivity中创建表单的应用程序。通过单击添加按钮,将显示一个列表视图(QuestionActivity)。在此活动中,出现一些问题。通过单击,假设是问题1(Question1活动),将出现一个编辑文本,用户可以在其中设置此表单的名称,该名称出现在MainActivity以及列表视图中。
我正在努力的一件事是,当我打开一个已经保存的表单时,我到达QuestionActivity中的问题列表。但是,当我单击问题1时,我已经保存在这里的数据都消失了。因此,我希望用户能够查看和更改他们已经保存的数据。但是我不确定该怎么做。
我可能应该在QuestionActivity中使用onResume方法,但我希望有人可以帮助我以适当的方式进行此操作。
这是我的代码:
public class MainActivity extends AppCompatActivity {
private ListView mListViewNotes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListViewNotes = findViewById(R.id.main_listview_notes);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_main_new_note:
Intent newActivity = new Intent(this, QuestionsActivity.class);
startActivity(newActivity);
break;
}
return true;
}
@Override
protected void onResume() {
super.onResume();
mListViewNotes.setAdapter(null);
ArrayList<Note> notes = Utilities.getAllSavedNotes(this);
if (notes ==null || notes.size() == 0) {
Toast.makeText(this, "no saved notes", Toast.LENGTH_SHORT).show();
return;
}else {
NoteAdapter na = new NoteAdapter(this, R.layout.item_note, notes);
mListViewNotes.setAdapter(na);
mListViewNotes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String fileName = ((Note)mListViewNotes.getItemAtPosition(position)).getDateTime()
+ Utilities.FILE_EXTENSION;
Intent viewNoteIntent = new Intent(getApplicationContext(),QuestionsActivity.class);
viewNoteIntent.putExtra("NOTE_FILE", fileName);
startActivity(viewNoteIntent);
}
});
}
}
}
public class QuestionsActivity extends AppCompatActivity {
ListView lv;
String[] charactersDC = {"Question 1", "Question 2", "Question 3", "Question 4", "Question 5"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_questions);
lv = (ListView) findViewById(R.id.idListView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, charactersDC);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(position == 0)
{
String stringFromQuestion1Result = ((Note)lv.getItemAtPosition(position)).getDateTime()
+ Utilities.FILE_EXTENSION;
Intent myIntent = new Intent(QuestionsActivity.this, Question1.class);
myIntent.putExtra("SAVED_FILE", stringFromQuestion1Result);
startActivityForResult(myIntent,0);
}
if(position == 1)
{
Intent myIntent = new Intent(QuestionsActivity.this, Question2.class);
startActivityForResult(myIntent, 0);
}
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == request_Code) {
if (resultCode == RESULT_OK) {
String stringFromQuestion1Result = data.getData().toString();
}
}
}
}
public class Question1 extends AppCompatActivity {
private EditText mEtTitle;
private String mNoteFileName;
private Note mLoadedNote;
private String mShowSavedData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question1);
mEtTitle = (EditText) findViewById(R.id.note_et_title);
mNoteFileName = getIntent().getStringExtra("NOTE_FILE");
if(mNoteFileName !=null && !mNoteFileName.isEmpty()) {
mLoadedNote = Utilities.getNoteByName(this, mNoteFileName);
if(mLoadedNote !=null) {
mEtTitle.setText(mLoadedNote.getTitle());
}
}
mShowSavedData = getIntent().getStringExtra("SAVED_FILE");
if(mShowSavedData !=null && !mShowSavedData.isEmpty()) {
mLoadedNote = Utilities.getNoteByName(this, mShowSavedData);
if (mLoadedNote !=null) {
mEtTitle.setText(mLoadedNote.getTitle());
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_note_new, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_note_save:
saveNote();
break;
}
return true;
}
private void saveNote() {
Note note;
if(mLoadedNote ==null) {
note = new Note(System.currentTimeMillis(), mEtTitle.getText().toString());
}else {
note = new Note(mLoadedNote.getDateTime(), mEtTitle.getText().toString());
}
if (Utilities.saveNote(this, note)){
Toast.makeText(this, "saved", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "not enough space", Toast.LENGTH_SHORT).show();
}
setResult (Activity.RESULT_OK, mEtTitle.getText().toString());
finish();
}
}
public class Utilities {
public static final String FILE_EXTENSION = ".bin";
public static boolean saveNote(Context context, Note note) {
// DELETED -> String fileName = String.valueOf(note.getDateTime()) + FILE_EXTENSION;
String stringFromQuestion1Result = String.valueOf(note.getDateTime()) + FILE_EXTENSION;
FileOutputStream fos;
ObjectOutputStream oos;
try {
fos = context.openFileOutput(stringFromQuestion1Result, context.MODE_PRIVATE);
oos = new ObjectOutputStream(fos);
oos.writeObject(note);
oos.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
return false; //tell user something went wrong
}
return true;
}
public static ArrayList<Note> getAllSavedNotes(Context context){
ArrayList<Note> notes = new ArrayList<>();
File filesDir = context.getFilesDir();
ArrayList<String> noteFiles = new ArrayList<>();
for(String file : filesDir.list()) {
if(file.endsWith(FILE_EXTENSION)) {
noteFiles.add(file);
}
}
FileInputStream fis;
ObjectInputStream ois;
for(int i = 0; i < noteFiles.size(); i++) {
try{
fis = context.openFileInput(noteFiles.get(i));
ois = new ObjectInputStream(fis);
notes.add((Note) ois.readObject());
fis.close();
ois.close();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}
return notes;
}
public static Note getNoteByName(Context context, String fileName) {
File file = new File(context.getFilesDir(), fileName);
Note note;
if(file.exists()) {
FileInputStream fis;
ObjectInputStream ois;
try{
fis = context.openFileInput(fileName);
ois = new ObjectInputStream(fis);
note = (Note) ois.readObject();
fis.close();
ois.close();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
return null;
}
return note;
}
return null;
}
}
答案 0 :(得分:0)
为Question活动捕获startActivityForResult的结果值。即,将onActivityResult添加到您的QuestionActivity中,如下所述: Android: how to make an activity return results to the activity which calls it? 在您的Question1活动中,在调用finish()之前,设置结果值:
setResult(Activity.RESULT_OK, mEtTitle.getText().toString());
finish();
在您的QuestionActivity中,添加方法
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == request_Code) {
if (resultCode == RESULT_OK) {
String stringFromQuestion1Result = data.getData().toString();
}
}
}
将结果存储在QuestionActivity中,并使用活动Intent将其传递回去。类似于您将“ NOTE_FILE”数据传递给QuestionActivity的方式,您需要将要在每个Question1活动中显示的数据传递给它,并加载所提供的值。
Intent myIntent = new Intent(QuestionsActivity.this, Question1.class);
myIntent.putExtra("Question1String", stringFromQuestion1Result);
startActivityForResult(myIntent, 0);