Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add new items to top of list view on Android?

Android has the transcript mode to allow to automatically scroll a list view to the bottom when new data is added to the adapter.

Can this be somehow reversed so that new items are automatically added at the top of the list ("inverse transcript mode")

Method stackFromBottom seems about right, but does not do the auto-scrolling on input change.

Does anyone have some example code where a list is constantly adding stuff that gets always inserted at the top? Am I on the right track here?

Update

Thanks for the answers, that made me think more. Actually. I want to have new entries to appear at the top, but the screen still show the item the user is looking at. The user should actively scroll to the top to view the new items. So I guess that transcript mode is not what I want.

like image 279
Heiko Rupp Avatar asked May 05 '11 20:05

Heiko Rupp


People also ask

How do I edit list view in android?

OnItemClickListener MshowforItem = new AdapterView. OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ((TextView)view). setText("Hello"); } };

How do I create a custom list view?

What is custom listview? Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

What is custom list in Android?

Android Custom ListView (Adding Images, sub-title)After creating simple ListView, android also provides facilities to customize our ListView. As the simple ListView, custom ListView also uses Adapter classes which added the content from data source (such as string array, array, database etc).


2 Answers

Hmm, well, if I was going to try this, I'd do something like the following:

List items = new ArrayList();  //some fictitious objectList where we're populating data for(Object obj : objectList) {     items.add(0, obj);     listAdapter.notifyDataSetChanged(); }  listView.post(new Runnable() {     @Override     public void run() {         listView.smoothScrollToPosition(0);     } } 

I don't know for certain that this will work, but it seems logical. Basically, just make sure to add the item at the beginning of the list (position 0), refresh the list adapter, and scroll to position (0, 0).

like image 119
Kevin Coppock Avatar answered Sep 22 '22 23:09

Kevin Coppock


instead of this:

items.add(edittext.getText().toString()); adapter.notifyDataSetChanged(); 

you should try that (works for me):

listview.post(new Runnable() {             @Override             public void run() {                 items.add(0, edittext.getText().toString());                 adapter.notifyDataSetChanged();                 listview.smoothScrollToPosition(0);             }             }); 
like image 38
elementstyle Avatar answered Sep 19 '22 23:09

elementstyle