好的,我看到很多关于堆栈溢出的问题,关于通过意图单击它时将数据从列表视图传递到另一个类,但我的问题是不同的。所以我有列表项,当我单击打开一个笔记类时,有列表标题和正文,标题与单击的列表项相同。在这个类中,我有一个打开另一个类的按钮,我也需要在这里传递标题,但我一生无法弄清楚它在 SQL Lite DB 中的位置,它是如何传递的以及如何通过它传递一个按钮。
这是列表项的 onclick 侦听器:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent i = new Intent(this, NoteEdit.class);
i.putExtra(NotesDbAdapter.KEY_ROWID, id);
startActivityForResult(i, ACTIVITY_EDIT);
}
这是接受这个并显示标题、正文和按钮的类,我还需要传递标题数据,我在按钮上方评论过:
public class NoteEdit extends Activity {
private EditText mTitleText;
private EditText mBodyText;
private Long mRowId;
private NotesDbAdapter mDbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDbHelper = new NotesDbAdapter(this);
mDbHelper.open();
setContentView(R.layout.note_edit);
setTitle(R.string.edit_note);
mTitleText = (EditText) findViewById(R.id.title);
mBodyText = (EditText) findViewById(R.id.body);
Button confirmButton = (Button) findViewById(R.id.confirm);
Button button1 = (Button) findViewById(R.id.butt);
mRowId = (savedInstanceState == null) ? null :
(Long) savedInstanceState.getSerializable(NotesDbAdapter.KEY_ROWID);
if (mRowId == null) {
Bundle extras = getIntent().getExtras();
mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID)
: null;
}
populateFields();
confirmButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
setResult(RESULT_OK);
finish();
}
});
//HERE IS THE BUTTON WHICH I USE TO GET TO THE NEXT ACTIVITY, I NEED TO PASS THE DATA IN TITLE FROM HERE!!
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent openNext = new Intent("com.timer.RUNNING");
startActivity(openNext);
}
});
}
private void populateFields() {
if (mRowId != null) {
Cursor note = mDbHelper.fetchNote(mRowId);
startManagingCursor(note);
mTitleText.setText(note.getString(
note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
mBodyText.setText(note.getString(
note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
saveState();
outState.putSerializable(NotesDbAdapter.KEY_ROWID, mRowId);
}
@Override
protected void onPause() {
super.onPause();
saveState();
}
@Override
protected void onResume() {
super.onResume();
populateFields();
}
private void saveState() {
String title = mTitleText.getText().toString();
String body = mBodyText.getText().toString();
if (mRowId == null) {
long id = mDbHelper.createNote(title, body);
if (id > 0) {
mRowId = id;
}
} else {
mDbHelper.updateNote(mRowId, title, body);
}
}
}
我希望这个问题是有道理的,当我不得不从列表中的 onclick 到仍然使用按钮传递该信息时,我陷入了困境。任何帮助是极大的赞赏!