Load more messages in listview implementation in android - 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

Related

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.

List view postion comes to top after append data

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!

Single ListView Adapter to serve different data to single ListView

I have my own custom ListView adapter which gets say for example 100 records. Now, out of these 100 records I want to show only select records in ListView. These select records could be in random order.
I have four such buttons and every time I click button I want to populate different records from adapter without making a new request again.
Any idea how can I achieve this?
Make two lists inside your custom adapter.
One - with all your records. And one - with records, that you want to display.
On click, you fill second list with data from first list somehow, call notifyDataSetChanged(). On other click, clear that list, fill with another records, and call notifyDataSetChanged() again.

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

get text from specific position in listview android

I have a ListView with custom_row , every row has a textView1 and a textView2 , the list has now 2 records , and i have a button that is not on the list.
When i click the button i want to get the text from textView2 of the 2 records.
Is it possible?
I would take a shortcut, you ListView is being populated by an Adapter that uses a dataset. This dataset can be almost any datastructure such as Array, ArrayList, etc.
The layout you define, such as custom_row in you case only defines the structure of your view i.e. "where" items will show within on item on the list.
On the other hand, it is still your responsibility to tell the ListView "what" to show within the textView1 and textView2. You do this using the Adapter which connects the ListView to the dataset. More often than not, the ListView is a one-to-one mapping of the dataset i.e. the first item on the list is the first item in you dataset (I don't know what you are using for only two items, might be an array).
The ListView calls getCount() on the Adapter to find out how many total views there will be. It then call getView() for each view to be shown on the screen. It is in this method that you define what will actually show in a single view on the list (your custom_row).
Now you would know which entry of the dataset is supposed to populate which view in the ListView so you can just read it off there. For example, if your getView() does:
textView2.setText(getItem(position).getSomeTextField());
And the original dataset is an ArrayList named listDataSet
You could just do listDataSet.get(2).getSomeTextField()
NOTE: You will have to manage the scope of the dataset so that it's visible from wherever you are calling.
Get back your ListView (maybe already stored in an object thanks to findViewById, or by calling getListView() on your ListActivity).
Then call getItemAtPosition() on your list view, with the position you want.

Categories

Resources