2

I'm making an app that receive datas from a webservice with an android app that have 2 views.

The first ask the login and the password. Then the activity request to the webservice, and if there is datas, he create an intent that shows the datas in a listview:

Main

private ArrayList accountlist = new ArrayList();
...
accountlist.add(...)
...
Intent intent = new Intent(Main.this, Second.class);
intent.putExtra("accountlist", accountlist);
intent.putExtra("login", loginEditText.getText().toString());
intent.putExtra("pass", passwordEditText.getText().toString());
startActivity(intent);

Second

private ArrayList accountlist;
...
public void onCreate(Bundle savedInstanceState) {
 ...
 setListAdapter(new ArrayAdapter<String>(this, R.layout.results,accountlist));
 ...
}
private void refresh() {
 ...
 accountlist.add(...)
 setListAdapter(new ArrayAdapter<String>(this, R.layout.results,accountlist));
}

The result is that I have the items that are addes into the current list (e.g I have 2 items, if I refresh 4 times, I will have 10 items in the view and I wand only the 2 last)

If you have an idea :D

clement
  • 4,204
  • 10
  • 65
  • 133

2 Answers2

0

you can't pass an ArrayList directly between activity, you have to go to kind of object by serializing. read this: How do I pass an object from one activity to another on Android?

Community
  • 1
  • 1
julien dumortier
  • 1,303
  • 1
  • 13
  • 28
  • 1
    ArrayList implements Serializable, so its perfectly valid to pass it between activities. – grassyburrito May 22 '12 at 20:35
  • @julien : It's not the problem, it's work perfectly, but in the refresh method (2nd class/intent), I want to delete and clear the items already on the list and then add the news one (end not just add new ones) – clement May 22 '12 at 20:38
  • @clement, in the refresh() method why don't you do a accountlist.clear() before you do a accountlist.add(..). – grassyburrito May 22 '12 at 20:46
  • @Prabhu : it was not in the refresh method but in the first intent (not the second one) Thanks to illuminate me :-) – clement May 22 '12 at 21:28
0

Okay, I just used the

accountlist.clear();

methid just after

Intent intent = new Intent(Main.this, Second.class);
intent.putExtra("accountlist", accountlist);
intent.putExtra("login", loginEditText.getText().toString());
intent.putExtra("pass", passwordEditText.getText().toString());
startActivity(intent);

to clear the list that will be re-used to launch the intent!

clement
  • 4,204
  • 10
  • 65
  • 133