Display list passed to listview and return id of selected item to previous activity
I am fairly new to Android development, and I would like to make a simple note-taking app as a learning exercise.
I have an activity with an edittext and a menu. when the menu is clicked I would like to display a second activity (or similar) that l开发者_如何学Cets the user select a note that then is returned to the edittext to be edited.
So far all the tutorials I can find use a hard-coded list or a list in a resources file, mine has to be more dynamic.
All help is greatly appreciated as I want to get coding!
Cheers,
Will.
I won't give you complete code as you are learning you should explore it yourself but I would like to give you some hints.
From your first Activity your should start second activity using startActivityForResult which starts an activity and that started activity will return some results to your first activity.
Another thing you can use is PutExtra which can be used for passing some data from one activity to another.
I will give you a dummy way
Create a class theApp
public class theApp extends Application {
private String Note;
public String getNote(){
return Note;
}
public void setNote(String Note){
this.Note = Note;
}
}
In the manifest (change the android:name="theApp")
<application android:icon="@drawable/icon" android:label="@string/app_name" android:name="theApp">
In the first activity (to return to the second activity):
Intent myIntent = new Intent(FIRSTACTIVITY.this,CALLED_ACTIVITY.class);
Bundle bundle = new Bundle();
bundle.putString("key1", whatever);//if you want to send some string
bundle.putString("key2", whatever);//you can also send integers and others using putInt and others
myIntent.putExtras(bundle);
startActivityForResult(myIntent, 0);
In the second activity (to return to the first activity):
((theApp)getApplicationContext()).setNote(a_STRING);
setResult(ANY_INTEGER);
finish();
Finally in the first activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// use ((theApp)getApplicationContext()).getNote();
// if you want you can use ANY_INTEGER (which was set in the second activity using setResult
// Access ANY_INTEGER using resultCode
}
I think JavaNut13 is more concerned about the views in his list and not activities.
@JavaNut13, take a look at http://thinkandroid.wordpress.com/2010/01/13/custom-baseadapters/
精彩评论