We're All Mad Here
We're All Mad Here

Reputation: 1554

Add new items on top of list view

Because android automatically moves new items to the bottom of list view I want that to be reversed. In any case my condition is met, I want to add new items on top of list view. I have seen this post here but I don't know how to add that to my code, here it is:

if(condition){
listView = (ListView) findViewById(R.id.ListView1);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            this, R.layout.list_b_text, R.id.list_content, ArrayofName);
    listView.setAdapter(adapter);
}

Upvotes: 0

Views: 2200

Answers (1)

Shahzeb
Shahzeb

Reputation: 3734

Just simply add every item at position 0 of your ArrayList so when you call listView.notifyDataSetChanged(); it will show latest items on top.

for (Object obj : objectList) {
    ArrayofName.add (0, obj); // this adds new items at top of ArrayList
}

objectList is basically an ArrayList or List of Object or String (whatever is your case). If you want to add items one by one, remove for loop. This loop actually iterates every item of objectList and adds it in your ArrayList at top position.

Upvotes: 3

Related Questions