• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps listview update not working properly

jiafish

Lurker
Aug 17, 2012
4
0
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();

}
}
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones