简体   繁体   中英

Android - Editing ListAdapter Data from another activity

I have a simple application that has 3 activities. The third activity is a ListActivity that lists movie titles and their respective gross and year, choosing any movie will display the values in the first activity. The second activity is where the user can add a movie to the ListActivity by filling up EditText fields and this is where I'm having problems.

So far, the examples I see involve use this line of code:

        SampleCustomAdapter adapter = new SampleCustomAdapter(results);
        setListAdapter(adapter);

        adapter.notifyDataSetChanged();

However, this line of code doesn't work for my case since the class in the second activity extends Activity and NOT ListActivity.

So far, this is the code I have when I attempt to add the new entries to the List.

//if all edit text fields have values and are valid inputs  
if( titleFlag == 1 && grossFlag == 1 && yearFlag == 1){
    //fix stuff here
    //TODO ADD THE NEW MOVIE TO THE ARRAY IN STRINGS.XML

    ArrayList<MyMovies> movieList = new ArrayList<MyMovies>();
    MyMovies newMovie = new MyMovies();

    newMovie.setMovie(title);
    newMovie.setGross(gross);
    newMovie.setYear(year);
    movieList.add(newMovie);


    //go back to the main page after adding
    Intent intent = new Intent(this, com.Android.Lab7.Lab7_084106.class);
    startActivity(intent);
    finish();

}

I also tried adding the adapter.notifyDataSetChanged(); in the third activity after generating the list but to no avail. Oh, the third activitys' onCreate looks something like this:

@Override
public void onCreate(Bundle savedInstanceState) {

    //create stuff
    super.onCreate(savedInstanceState);

    ArrayList<MyMovies> movieList = new ArrayList<MyMovies>();

    String[] movieArray = getResources().getStringArray(R.array.movieArray);
    String[] grossArray = getResources().getStringArray(R.array.worldwideGross);
    String[] yearArray = getResources().getStringArray(R.array.yearArray);

        ArrayList<MyMovies> results = new ArrayList<MyMovies>();
        // make sure the arrays have the same length
        for (int i = 0; i < movieArray.length; i++) {
            MyMovies sr = new MyMovies();
            sr.setMovie(movieArray[i]);
            sr.setGross(grossArray[i]);
            sr.setYear(yearArray[i]);
            results.add(sr);

       }


        SampleCustomAdapter adapter = new SampleCustomAdapter(results);
        setListAdapter(adapter);



    ListView lv = getListView();
    lv.setTextFilterEnabled(true);
    adapter.notifyDataSetChanged();

    //set stuff such that Page2 sends back a result to page 1
    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            sendResult(position);   
        }
    });


}

I used the strings resource to store the array of titles, gross, and year since there's a lot of them and it's not practical that I hardcode them.

So basically I'm stuck on how I can update the List in the third activity from the second activity.

Declare this in the third Activity.

public static String title, gross, year;
public static boolean newMovieAdded=false;

Then when you are in the second activity do the following

thirdActivity.title = MyEditText1.getText().toString();

thirdActivity.gross= MyEditText2.getText().toString();

thirdActivity.year= MyEditText3.getText().toString();

if(!year.equals(""))

newMovieAdded=true;

thats all. when you go to the third activity just write your code

     ArrayList<MyMovies> movieList = new ArrayList<MyMovies>();

    if(newMovieAdded){
        MyMovies newMovie = new MyMovies();
        newMovie.setMovie(title);
        newMovie.setGross(gross);
        newMovie.setYear(year);
        movieList.add(newMovie);
        newMovieAdded = false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM