Display two Reclerview in single activty with same recyclerview adapter - android

I have two List coming from webservice Like past and future.Sometimes both List have data and sometime only one list have data. And how set the differt RecyclerView for pastList and FuturList with same adapter.

As long as your model objects are the same, it sounds like you need to have two versions of the adapter, where you can assign each list of data to each different adapter.
So if your adapter was MyObjectRecyclerViewAdapter, you could make two instances - pastObjectsAdapter = MyObjectRecyclerViewAdapter();
futureObjectsAdapter = MyOjbectRecyclerViewAdapter();
Then assign one to each of your RecyclerViews and update the list on each with your lists from your webservice.

Related

Best strategy to create a ListView of forms in Android?

I need to create a dynamic list of forms. The user would be able to input data into each element of the list (which would contain an editable text box and other elements).
What is the best approach? How would I access the inputted information in each list element? Would each list element have to be its own Fragment?
1) Decide what elements you need in each list item
2) Create a data class with the member variables to how your data. one data object will be initialized per listview item //note this is not required, but it can help.
3) Create a layout (list_view_item_layout.xml) for your listview items
4) Declare your ListView in your layout.xml, ensure that you give it an android:id.
5) Declare and initialize an array of your data objects (or just an array of data) that holds all the data objects that you want displayed in your list
5) Create your MyListViewAdapter.class that extends BaseAdapter. this class will inflate your list_view_item_layout and populate the views with the data that is in your data array
6) call ListView#setAdapter(); passing in myListViewAdfapter, context, R.id.document_list_view //I htink that is correct
Thats about all there is to it.
Look for tutorials for custom list view adapters, there are a billion + tutorials

how to populate listview one by one not at once

I want to populate my custom listview with some lists of data but data is retrieving from my server. In my case listview is populated when all data from server is retrieved which take some time. I want to load my listview in the ways one by one each time the data retrieved.
In my current logic I use notifyDataSetChanged() for updating the adapter but in this case it refresh all the data at once. Is there any way to load custom list view one by one asynchronously?.

How to show All list items in child ListView-using cwac-merge-1.0.4 jar

I am using two ListView in same screen,Because of using two Listviews likely Parent ListView and Child ListView,that Child ListView Adapter class doesn't show all List Values in Child ListView.It showed only 0 th(first) position in Child ListView,for this above issue I have used the answer code from this Android list view inside a scroll view link.now I can see all list values in my Child ListView but It is not showing all my List values in same time which I had grabbed from Web service call.It showed first value only in list because the List View height was set to list's single item height.If I scroll down the Child ListView I am able to see all values in ListView.What I need is If i get five list values from Web service It has to show all five items in same time.
note: If I set hard coded values for List,It showed all items in Child ListView at one time.
Edited
How I achieved it using MergeAdapter
This createAdapter method would return Adapter object and I have set that Adapter into my Listview using setAdapter(createAdapter(new ArrayList<T>()))
private ListAdapter createAdapter(List<T> items) {
mergeAdapter = new MergeAdapter();
mergeAdapter.addAdapter(endlessFeedAdapter);
return mergeAdapter;
}
What I need is
I Have been suggested to use ExpandableListView instead of using two ListViews.
If I use ExpandableListView I will have to change the createAdapter method return type into ExpandableListAdapter for this I used below code
private ExpandableListAdapter createAdapter(List<T> items) {
mergeAdapter = new MergeAdapter();
mergeAdapter.addAdapter(endlessFeedAdapter);
return (ExpandableListAdapter) mergeAdapter;
}
but it showed the below Excaption
Caused by: java.lang.ClassCastException: com.commonsware.cwac.merge.MergeAdapter cannot be cast to android.widget.ExpandableListAdapter
Values from Web service
Hard coded values
What is stopping you to make 2 different adapters and adding them to MergeAdapter? You can add multiple adapters to MergeAdapter and multiple views. In that case there is no need to use 2 Listviews.
mergeAdapter.addAdapter(adapterHeading);
mergeAdapter.addView(someView);
mergeAdapter.addAdapter(adapterFooter);
listView.setAdapter(mergeAdapter);

android clear arraylist or adapter?

After working on an app for a while I realize I use
adapter.clear()
and
arraylist.clear()
I can see both are working just fine, I would like to know the difference between the two!
Both are called before I start and asyncTask that updates my list with information from my server!
You should not be clearing the ArrayList directly. The ArrayAdapter makes absolutely no guarantees that it maintains the same referenced list given to it. In fact it will change when you perform a search with it's filter. Which would make arrayList.clear() fail.
Rule of thumb, if you ever need to mutate or retrieve the associating data...do it directly from the adapter. Not the list you used to construct it.
Adapter = it contains copies of diff views,arrays
aaraylist holds the data which we want to display in our view.
ex: arraylist<HashMap<String,String>> ah= new ArrayList<HashMap<String,String>>();
the above list contains hashmap
if i clear the arraylist there will be no data to show on listview or gridview so it will be empty
if i clear adapter than it will destroy the copies of array and views so the output will be same

How to create custom dynamic list in android?

I want to create a custom list in which items are added or removed dynamically (say when a button is tapped). The problem is I have very little knowledge of lists in android. I have gone through various tutorials on creating a custom list in android but none of them shows how to dynamically add contents to it
What I know so far:
1) I have to create a model class to store data.
2) I have to create an adapter class.
3) Pass the objects of the model class as an arraylist to the adapter.
3) Bind listview to the adapter
What's confusing me:
1) I know I have to create an apapter class, but what's really confusing me is what kind of adapter ? i.e. ArrayAdapter, BaseAdapter ??
2) What and How will I feed adapter? I will be fetching data from the Sql lite database and I want the results to be displayed in my custom made list.
3) How will I update my list when a new record is added to the database ? I know how to populate listview from a static array but its of no use in my project.
I need little guidance where should I start from ?
1) You can use ArrayAdapter.
2) After you create your own arraylist, you can pass it for first time, listview.setAdapter(...
3) After you refresh your data you can call this method, ((ArrayAdapter)listView.getAdapter).notifyDataSetChanged(). This will ensure your listview refresh.
The below link is a good example:
https://github.com/thecodepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView

Categories

Resources