EDIT: I have figured out my main issue, but still have one concern. Everything below this paragraph has been solved. I am now able to populate fields on one activity after selecting from a list in the other. The only problem is that after I "quick add", I get stuck with the quick add screen between two versions of the other screen. In other words, say I press the quick add button and my listview activity comes up. When I select an item, it essentially goes forward and creates another instance of the original activity. So if you hit the back button, you have to go back through the listview, then back to the original instance of the original activity. Is there any way to avoid this?
I have a button in one activity that opens a list view in another that's populated with two columns from a database - name and price (this was modeled on the Notepad tutorial). The list is meant as a "quick add" option. What I want it to do is, when pressed, populate the "name" and "price" EditTexts in the previous activity with the corresponding database entry.
So as per the tutorial, this is what I have in my ItemPicker class:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Cursor c = mNotesCursor;
c.moveToPosition(position);
Intent i = new Intent(this, NewEntry.class);
i.putExtra(ItemsDbAdapter.KEY_ROWID, id);
i.putExtra(ItemsDbAdapter.KEY_NAME, c.getString(
c.getColumnIndexOrThrow(ItemsDbAdapter.KEY_NAME)));
i.putExtra(ItemsDbAdapter.KEY_PRICE, c.getString(
c.getColumnIndexOrThrow(ItemsDbAdapter.KEY_PRICE)));
startActivityForResult(i, ACTIVITY_AUTO);
}
I'm completely lost when it comes to retrieving that info in the NewEntry class. I figure it's a matter of getting 开发者_如何学Gothe extras, but I don't even know how to start formatting the code.
Also, I need it to switch back to the NewEntry class activity automatically. Is that as easy as adding a startActivity() to this method? Thanks.
EDIT: Nevermind, figured this one out finally. Here's the code in my other activity:
Bundle extras = getIntent().getExtras();
if (extras != null) {
editName.setText(extras.getString(ItemsDbAdapter.KEY_NAME));
editPrice.setText(extras.getString(ItemsDbAdapter.KEY_PRICE));
}
WHy can't you just use finish() on the listactivity?
THat's what I do here... Or maybe I didn't understand your problem.
Don't use startactivityforresult on the second activity. Just use finish(). ;-)
// edited:
And by the way, you don't create a new Intent. You use Intent i = getIntent(); to get the original Intent. You're doing it wrong!
The purpose of the Intent on the second class is not to start another activity. It is to return the results to the original intent. Think of intents as bridges. When you are at the other side you don't create a new bridge, you return the results to the original bridge and return (finish()) from it.
That's why you need Intent i = getIntent() instead of new Intent, and finish() instead of startactivityforresult (which creates a new bridge instead of returning from the first one).
精彩评论