Please excuse my own stupidity. This problem was not caused by the specific function, but my own failed logic. Mods please delete this thread.
/*
So i have a listview and is trying to update it. Listview is populated by a ArrayList. When i want to update it, i call notifyDataSetChanged(). But what happens is that the new data is simply added on top of the old one.
Lets say the old one is a,b. the updated one is a,b,c. what i get is a,b,a,b,c.
I've looked into many other methods such as invalidateview, refreshDrawableState, setAdapter(null) etc, but none worked. heres a simplified version of my code, thank you in advance.
*/
public class MainActivity extends Activity
{
ArrayList<String> names = new ArrayList<String>();
ArrayAdapter arrayAdapter = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv1 = (ListView) findViewById(R.id.listview);
arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, names);
lv1.setAdapter(arrayAdapter);
}
//code here to edit the ArrayList names.
public void onResume()
{
super.onResume(); // Always call the superclass method first
//the activity need to be updated everytime it resumes.
arrayAdapter.notifyDataSetChanged();
}
}
/*
So i have a listview and is trying to update it. Listview is populated by a ArrayList. When i want to update it, i call notifyDataSetChanged(). But what happens is that the new data is simply added on top of the old one.
Lets say the old one is a,b. the updated one is a,b,c. what i get is a,b,a,b,c.
I've looked into many other methods such as invalidateview, refreshDrawableState, setAdapter(null) etc, but none worked. heres a simplified version of my code, thank you in advance.
*/
public class MainActivity extends Activity
{
ArrayList<String> names = new ArrayList<String>();
ArrayAdapter arrayAdapter = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv1 = (ListView) findViewById(R.id.listview);
arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, names);
lv1.setAdapter(arrayAdapter);
}
//code here to edit the ArrayList names.
public void onResume()
{
super.onResume(); // Always call the superclass method first
//the activity need to be updated everytime it resumes.
arrayAdapter.notifyDataSetChanged();
}
}