我有一个关于写笔记的Android应用程序。当我按 Second2 活动中的编辑按钮时,它将移动到 Second3 活动而没有任何问题(在 Second3 活动我可以编辑该注释而不用任何问题)。但是当我按下“保存”按钮时,它将返回 Second2 ,但没有任何内容出现!它只是一个空的活动!请帮帮我!
MainActivity类:
package com.twitter.i_droidi.mynotes;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.List;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
public class MainActivity extends ActionBarActivity implements AdapterView.OnItemClickListener {
ListView lv;
NotesDataSource nDS;
List<NotesModel> notesList;
String[] notes;
int i;
ArrayAdapter<String> adapter;
AdView mAdView;
AdRequest adRequest;
public static long back_pressed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nDS = new NotesDataSource(this);
lv = (ListView) findViewById(R.id.lv);
nDS.open();
notesList = nDS.getAllNotes();
nDS.close();
notes = new String[notesList.size()];
for (i = 0; i < notesList.size(); i++) {
notes[i] = notesList.get(i).getTitle();
}
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,
android.R.id.text1, notes);
lv.setAdapter(adapter);
registerForContextMenu(lv);
lv.setOnItemClickListener(this);
mAdView = (AdView) findViewById(R.id.adView);
adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
@Override
public void onBackPressed()
{
if (back_pressed + 2000 > System.currentTimeMillis()) super.onBackPressed();
else Toast.makeText(this, "Press once again to exit!", Toast.LENGTH_SHORT).show();
back_pressed = System.currentTimeMillis();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent nView = new Intent(this, Second2.class);
nView.putExtra("id", notesList.get(position).getId());
nView.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(nView);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_delete, menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.delete:
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
nDS.open();
nDS.deleteNote(notesList.get(info.position).getId());
notesList = nDS.getAllNotes();
nDS.close();
notes = new String[notesList.size()];
for (i = 0; i < notesList.size(); i++) {
notes[i] = notesList.get(i).getTitle();
}
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,
android.R.id.text1, notes);
lv.setAdapter(adapter);
registerForContextMenu(lv);
lv.setOnItemClickListener(this);
Toast nDelete = Toast.makeText(this, R.string.deleted, Toast.LENGTH_LONG);
nDelete.show();
}
return super.onContextItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mainMenuNewNote:
Intent nNote = new Intent(this, Second.class);
startActivity(nNote);
return true;
case R.id.mainMenuAbout:
AlertDialog.Builder aboutDialog = new AlertDialog.Builder(this);
aboutDialog.setTitle(getString(R.string.about_title));
aboutDialog.setMessage(R.string.about_body);
aboutDialog.setIcon(R.drawable.my_notes);
aboutDialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface aboutDialog, int witch) {
// Do Not Do Anything.
}
});
aboutDialog.show();
return true;
}
return super.onOptionsItemSelected(item);
}
}
第二课:
package com.twitter.i_droidi.mynotes;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;
public class Second extends ActionBarActivity {
NotesDataSource nDS;
EditText noteTitle;
EditText noteBody;
int id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent in = getIntent();
id = in.getIntExtra("id", 0);
noteTitle = (EditText) findViewById(R.id.note_title);
noteBody = (EditText) findViewById(R.id.note);
nDS = new NotesDataSource(this);
nDS.open();
NotesModel note = nDS.getNote(id);
nDS.close();
noteTitle.setText(note.getTitle());
noteBody.setText(note.getBody());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_second, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.secondMenuSave:
if (!noteTitle.getText().toString().isEmpty() && !noteBody.getText().toString().isEmpty()) {
nDS.open();
nDS.createNote(noteTitle.getText().toString(), noteBody.getText().toString());
nDS.close();
Toast nSave = Toast.makeText(this, R.string.saved, Toast.LENGTH_LONG);
nSave.show();
Intent nView = new Intent(this, MainActivity.class);
nView.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(nView);
} else {
Toast notSave = Toast.makeText(this, R.string.do_not_save, Toast.LENGTH_LONG);
notSave.show();
}
return true;
}
return super.onOptionsItemSelected(item);
}
}
第二课:
package com.twitter.i_droidi.mynotes;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
public class Second2 extends ActionBarActivity {
TextView viewNoteTitle;
TextView viewNoteBody;
int id;
NotesDataSource nDS;
NotesModel note;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second2);
Intent in = getIntent();
id = in.getIntExtra("id", 0);
viewNoteTitle = (TextView) findViewById(R.id.view_note_title);
viewNoteBody = (TextView) findViewById(R.id.view_note);
nDS = new NotesDataSource(this);
nDS.open();
note = nDS.getNote(id);
nDS.close();
viewNoteTitle.setText(note.getTitle());
viewNoteBody.setText(note.getBody());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_second2, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.second2MenuEdit:
Intent nEdit = new Intent(this, Second3.class);
nEdit.putExtra("id", note.getId());
nEdit.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(nEdit);
}
return super.onOptionsItemSelected(item);
}
}
第二课:
package com.twitter.i_droidi.mynotes;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;
public class Second3 extends ActionBarActivity {
NotesDataSource nDS;
EditText noteTitle;
EditText noteBody;
int id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent in = getIntent();
id = in.getIntExtra("id", 0);
noteTitle = (EditText) findViewById(R.id.note_title);
noteBody = (EditText) findViewById(R.id.note);
nDS = new NotesDataSource(this);
nDS.open();
NotesModel note = nDS.getNote(id);
nDS.close();
noteTitle.setText(note.getTitle());
noteBody.setText(note.getBody());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_second, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.secondMenuSave:
if (!noteTitle.getText().toString().isEmpty() && !noteBody.getText().toString().isEmpty()) {
nDS.open();
nDS.updateNote(id, noteTitle.getText().toString(), noteBody.getText().toString());
nDS.close();
Toast nSave = Toast.makeText(this, R.string.saved, Toast.LENGTH_LONG);
nSave.show();
Intent nView = new Intent(this, Second2.class);
nView.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(nView);
} else {
Toast notSave = Toast.makeText(this, R.string.do_not_save, Toast.LENGTH_LONG);
notSave.show();
}
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:1)
第二个活动从这两行获取来自Intent的数据:
Intent in = getIntent();
id = in.getIntExtra("id", 0);
因此,当您从第三个活动返回此活动时,您将笔记保存在以下行中:
Intent nView = new Intent(this, Second2.class);
nView.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(nView);
您必须像在Main Activity类中那样将ID添加到Intent中,您可以按照以下方式执行此操作,但是您在第三个活动中没有noteList
数组。
nView.putExtra("id", notesList.get(position).getId());
因此,获取活动数据的最简单方法是调用
finish();
这是因为第二个活动仍然在活动堆栈中而你不必再次启动它,你可以完成第三个活动而第二个活动就会显示,因为它是堆栈中的最后一个活动。但标题不会更新,因此您必须从下面的代码开始启动Activity:
保存完笔记后。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.secondMenuSave:
if (!noteTitle.getText().toString().isEmpty() && !noteBody.getText().toString().isEmpty()) {
nDS.open();
nDS.updateNote(id, noteTitle.getText().toString(), noteBody.getText().toString());
nDS.close();
Toast nSave = Toast.makeText(this, R.string.saved, Toast.LENGTH_LONG);
nSave.show();
Intent nView = new Intent(this, Second2.class);
nView.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
nView.putExtra("id", id);
startActivity(nView);
finish();
} else {
Toast notSave = Toast.makeText(this, R.string.do_not_save, Toast.LENGTH_LONG);
notSave.show();
}
return true;
}
return super.onOptionsItemSelected(item);
}
对于你希望最后的问题,在第二个活动中覆盖onBackPressed方法并添加以下代码:
@Override
public void onBackPressed()
{
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}