Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Whats wrong in this code..

public class MoviesListActivity extends ListActivity {


private ArrayList<Movie> moviesList;
private ArrayAdapter<Movie> moviesAdapter;

@SuppressWarnings("unchecked")
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.movies_layout);

    moviesList = (ArrayList<Movie>) getIntent().getSerializableExtra("movies");

    moviesAdapter = new MoviesAdapter(this, R.layout.movie_data_row, moviesList);

    setListAdapter(moviesAdapter);

    if (moviesList!=null && !moviesList.isEmpty()) {
        longToast(String.valueOf(moviesList.size()));
        moviesAdapter.clear();
        moviesAdapter.notifyDataSetChanged();


            for (int i = 0; i < moviesList.size(); i++) {
                moviesAdapter.add(moviesList.get(i)); 

            }

        }

        moviesAdapter.notifyDataSetChanged();
}

after moviesAdapter.clear();, moviesList.size() become zero. and before clear code movieslist get some data. Now after removing moviesAdapter.clear(), application get force close... Pls help..Thanks

share|improve this question
1  
post logcat error –  shayan pourvatan Jun 24 at 6:08
    
Pasting the relevant excerpt from the crash log would help. –  Gagan Jun 24 at 6:09
    
i want to ask, after moviesAdapter.clear() , why moviesList.size() become zero. –  dheeraj92 Jun 24 at 6:13
    
Try to replace this line : moviesAdapter = new MoviesAdapter(this, R.layout.movie_data_row, new ArrayList<Movie>(moviesList)); –  Haresh Jun 24 at 6:18
    
You might want to check if the instance onto which moviesAdapter.clear() operates upon. I'm having a hunch that your MoviesAdapter might be directly referencing your moviesList, and therefore the clear is directly affecting your moviesList. I'm wondering though, why you're not first setting the data in the adapter before calling setListAdapter. Then you wouldn't need 2 moviesAdapter.notifyDataSetChanged calls. –  Xilconic Jun 24 at 6:19
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.