add items on top of the listview android - android

I have a listview and I am using list.add(mylist) and adapter.notifydatasetchanged() ,its adding the items below the existing list ,How Do I add the Items on Top of the List.
Eg: If I receive new msgs It should be on top of the existing list.
Thanks in Advance.

Use add(int index, E object). Where index = 0.

Actually, if you bind your adapter to your ListView, you can also just insert the item into your adapter with the position (in this case 0)
list.setAdapter(adapter);
...
adapter.insert(data, 0);
It shoudl update by itself because you had it bound

Related

How to clear the ExpandableListView?

I am using expandable list view as a tree view structure and it needs to be updated continuously but as I can not clear the current expandable list view, it shows the previous data along with new data. Like if I minimize and again open app there is duplicate data in list view.
yourExpandableListView.setAdapter((BaseExpandableListAdapter)null);
ExpandableListAdapter adapter = new ExpandableListAdapter(); //or what ever adapter you use/created listView.setAdapter(adapter)
Please try this

Add item to spinner after it has already been filled

I create a spinner somewhere in my code and fill it in with values
val dataAdapter = ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, some_list)
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
mySpinner?.adapter = dataAdapter
My Question is, How do I add more items to this spinner after I have applied the adapter in a completely different section of code in Kotlin? for example, after a server request
I understand you can access the adapter via
mySpinner?.adapter
But how do I add to it?
EDIT.
I Understand there is a solution in the following code
some_list.add("Apple");
dataAdapter.notifyDataSetChanged();
However this requires having access to 3 data items at a class or global level instead of just 1 (the spinner). I understand this may be the only way, it is just sub-optimal, thank you for your responses
you can create a new adapter with a the newest list and then assign it to the spinner , or you can create a custom adapter with your item list and then just add the new item to the list and call notifyDataSetChanged()

Add new items to the start of List and notify adapter

I have an ViewPagerAdapter, and a List with ViewPager items inside of it. In some moments, I have to add items to my list. I've add they by List.addAll(Collection), call notifyDataSetChanged(), and all is working well. But sometimes I need to add an items to start of my List. I can add they, but notifyDataSetChanged() isn't working in case when start of my list is chanegd.
Any ideas?
Try this code
List items = new ArrayList();
//some fictitious objectList where we're populating data
items.add(0, obj);
listAdapter.notifyDataSetChanged();
This may work. You need not to use Loop and Notify List again and again
Add new items to the start of List
use following code for adding the item to List in onCreate() or where you want.
listName.add("item-1");
listName.add("item-2");
.
.
listName.add("item-n");
notify adapter
after that, use notifyDataSetChanged() to intimate to listAdapter for new Items added in list,
listAdapter.notifyDataSetChanged();
hope this will helps you.

How to hide items of listview in android

I'm trying to develop an app in which i need to create a list which contain many items.
There is also some button represent categories , and i want to populate my list according to the category.
For example: if there are 10 items in the list out of which item 1,5,7,8 are belongs to 1st category and rest of them belongs to 2nd category,now if user press 1st category button then listview show items belonging to 1st category only.
How can I do it?
I think ExpandableListView will work for you. Refer this tutorial: http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
If you wish to be able to filter (hide/show) items in a ListView you can use an adapter, for example by creating your own implementation of BaseAdapter. This adapter has methods for sorting, clearing and adding elements to the dataset used by your ListView, as stated in this list tutorial. Just remember to call notifyDataSetChanged() after you've altered the dataset to refresh the ListView.
Setup example:
ListView list = (ListView)findViewById(R.id.list);
adapter = new MyAdapter(...); // Probably send along data
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
Filter example:
int[] newData = { 1, 5, 7, 8 };
adapter.clear();
for(int i : newData)
adapter.add(i);
adapter.notifyDataSetChanged();
You need to hide item in list (remove from list) before you set adapter. When your list is ready set notifyDataSetChanged() to adapter

ListView increasement and adapter changed dynamically

Friends. I am new android developer and asking question .I have searchd a lot,but didn't get the desired result.So, Here's my problem.
1. I have a ListView that conations a number of items
ListView
_________
item1
item2
item3
_________
2. When I click onItem click Listener, I get all the values of item conation(ItemDetail.java)
3. Now I have two buttons in (ItemDetail.java) buttonNext and buttonPrevious
so my query when I click buttonNext and buttonPrevious my listview item detail from list should be changed dynamically increment and decrement of position.
Thanks in advance friends.
You have to code on onResume of that List Activity.
Just update your adapter with the newly sorted data as you want and refresh the listview with that new data.
In case of any query, let me know.
Hope you got my point.
Did you try this:
OnClick() change collection elements order of the list you used in adapter (list that is binded). SwapElements(list, index1, index2)
you need to use runOnUiThread() to bind data of UI components.
runOnUiThread(new Runnable() { public void run() { refreshListview() } });
refreshListview() method is the one you are using for your refresh routine.

Categories

Resources