List view postion comes to top after append data - android

I have a list view that fetch data from server when it scroll to bottom. once the data received it pass to the adapter via an array list.
But when i set the array list values to adapter, it appends the data and automatically the position moves to top.
Could some one tell me what is the problem occured here?

I guess that you recreated new Adapter and set it to your listview.
If you do like that, your listview will scroll to top.
To keep your listview item position, just call mAdapter.notifyDataSetChanged(); after update your array list.
Hope it can help you!

Related

Get ListView item position by uri / by the data it contains

I have a ListView which I populate using a custom CursorAdapter.
Now I want to manually update just one specific item in the ListView. I have the content URI of that item. Is it possible to use just this info to get the position of the item in the listView?
If I have the position I can do something like
View v = mListView.getChildAt(itemPosition - mListView.getFirstVisiblePosition());
to update the view. But how can I get itemPosition?
I know that I get the position in the onItemClickListener, but I need to update the view without it being clicked.
Any help guys?
ListView does not support updating a single position.
You must update the adapter with the new data (even if you change a single position) and the, invoke mAdapter.notifyDataSetChanged()
If you get the View by its position (via listView.getChildAt()) will work. However, if you scroll up and down the list, that view will display the old data again because the adapter is not aware of the change (and it is the adapter which update the view contect view getView()).
When you invoke notifyDataSetChanged(), you are telling to the adapter that your data set has new info and the ListView/Adapter will re-draw the visible items (it won't re-draw whole list at once.. only the visible items).
You may want to consider to change to RecyclerView in the future. The BaseAdapter used in a RecyclerView support actions such as add/remove/update a single position.

updating adapter by data from ListView (not the opposite)

I have a long ListView, which its items are editable (simple strings).
The user can change the content as he/she wants.
My problem is, that when I want to retrieve the new data from the List view,
I can not reach the invisible items in the list.
How can I retrieve the data from these items without scrolling to these items?
Is there a way to fill the adapter ,with the new data the user entered to the list?
I think you can do that. if you have reference of Adapter for that list view
you can call getCount and then iterate and while calling getItem(pos) , to get data for all the positions in the list view.

Custom List View items

I am writing code for a listview which has fixed number of rows and when the user scrolls the listview, instead of moving the list elements (views), I want to change the content of the views where as the list elements (views) stay at the there position.
For this I am returning a number Eg. 5 in getCount(), however I am no clue on how can I change contents of list items while keeping the list items in a fixed position.
Any help is welcomed.
Regards
with getItemAtPosition()
you get the item at position, there you can change its content and then call notifyDataSetChanged() to signal the adapter that the data has changed.

How can i stop executing getview() method in android?

I have two list view for displaying data from sqlite database.I displayed data without any problem.in second list view i have header with four columns.When i click on first list view , that related data displayed in second list it displayed based on position.By default i displayed first list view of the zero position related data in second list. the problem is , while scrolling the first list view it automatically calls the zero position values but i need the data of whatever position i clicked on first list view.I displayed data like below.How i can do?
Please can any on help?
first list view:
first........
second........
third........
I displayed data based on selection of first list view.
Second list view:
header
id Name Contact
1 ram 12345
Thanking in Advance.
easy bruteforce way is to use flag like this..
public boolean flag=false; // declare this as class variable outside both listview adapter class
then in 1st list view getview() function
if(!flag){
//display 2nd list view contents on 0th position of 1st list
flag=true;
}
in onItemClick() of 1st list view
//display 2nd list view contens based on position

Load more messages in listview implementation in android

I display first 20 records fetched from server in a listview.
I have baseadapter which is binded to listview as an adapter, and i pass records fetched from server to this baseadapter.
I set a footer item as textview attached to listview, onclicklistener of same, it fetches new 20 records from server, then i add those 20 records and pass to baseadapter.
But it reloads whole listview again, and displays all 40 records but starting from list item 1.
I want to display whole list but cursor point should be from new items added in list, as similar to "Email application" in android.
In other words, only new items should be refreshed to
For that you need to call the below code when you notify your adapter.
listview.setSelectionFromTop();
It set your item from the top.
Your app can keep current listview position and after updting listview you can navigate to this position through setPosition method

Categories

Resources