is there a way to add data from cursor to viewpager, I have data in my cursor for example 4 items and I need to place each item on a page how can I do this using cursor and viewpager is it possible?
thanks.
Yes it is possible, you need a PagerAdapter, it will bind your PagerView with data and generate every "item" (every fragment or page) that shows up on the PagerView.
So first override a PagerAdapter, for example:
public class MyPagerAdapter extends PagerAdapter
Then you have to override the method that generates every item on the PagerAdapter, the instantiateItem() method. This method gives you an integer argument named position, it corresponds to the item number on the list of items that will show on your PagerView. Imagine a book, if every page is an item, the page number is the position.
So according to this position, every item will be different in a way that you must explain to the adapter through your code. To do this, you can use this position to access your list of data and create a correspondence between the list and the page.
Lets say you have a table Person with people names and you retrieve all entries, you create a List that holds all the names data from that cursor. You can now access your list using the item position, for example, list.get(position) and by retrieving the data from that particular list item you can create the individual page that you must return on instantiateItem. You can, for example inflate a layout, which contains a TextView called personName, then do something like personName.setText(list_name.get(position)). Now return the LayoutView. The Adapter will then create a view for every item on your List.
So you can do something like this:
ArrayList<String> names_list = new ArrayList<String>();
then store the cursor information on that List (You can do more than one list, and you can do it directly with a cursor using a CursorAdapter, this is just an example) and then instantiate your adapter:
MyPagerAdapter adapter = new MyPagerAdapter(context, layout, names_list);
For every item on the list, the adapter will run the instantiateItem method and create an item to populate a ViewPager, so now all you have to do is bind the Adapter with your ViewPager:
ViewPager pager = (ViewPager)findViewById(R.id.activity_search_viewpager);
pager.setAdapter(adapter);
This is just an example to kinda explain the idea of how a PagerAdapter works, there is a bit more to this subject than just this. Anyway, once you get confortable with the way things work, you can also check the CursorAdapter, so you can instantiate items directly from cursor data, no need for a list.
Here are some SDK references you should check:
http://developer.android.com/reference/android/support/v4/view/PagerAdapter.html
http://developer.android.com/reference/android/support/v4/widget/CursorAdapter.html
Good luck :)
Related
I have implemented a firebase list adapter to load a list of items (List A). My data structure is setup such that within List A, each item also contains a reference id to some information located somewhere else (isNice) in the database. Likewise:
ListA
- ObjA
- title : "hi"
- id : "ObjAid"
isNice
- ObjAid : "true"
I'm currently using another database operation to look up the id in the "isNice" child, passing the "ObjAid", the list position, and then a resultReceiver to get the result back. My problem is, when the resultReceiver get a resultData (the value "true"), I have no idea how modify the data within firebase list at the specific position.
My past experience is to use load my data into my own ArrayList and create a custom adapter for the listView, in that case, I could easily update the populated view as extra information is loaded. I would like to avoid setting up my own ArrayList to store the data for the time being, favoring the simplicity of the FirebaseListAdapter. All tips are appreciated, thx :)
After some trial and error, I've instead gone with the RecyclerView + FirebaseRecyclerViewAdapter approach. Using recycler is better for the long run too, in my use case the list could get to 3K+. Using RecyclerView.findViewHolderForAdapterPosition() I can reference the specific itemView after I get the data from the result receiver likewise:
// Reference the Item's view at the specific position
myViewHolder itemViewHolder = recyclerView.findViewHolderForAdapterPosition(position);
// Reference and set the views and here......
I have 10 items in my activity.
They are displayed in a RecyclerView.
The itms are stored in a list;
List<CustomItem> items = new ArrayList<>();
I pass them to the RecyclerView adapter like so:
recyclerView.setAdapter(new RVAdapter(this, items));
Now, whenever I do filter the items by a string query, so that the
recycler view displays a subset of the items by description, for instance,
I have to pass a filtered list of items to the recycler view, like so:
List<CustomItem> filteredItems = CustomItem.filterByQuery(items, query);
and then I:
adapter.setItemList(filteredItems);
notifyDataSetChanged();
-> which sets the filtered items in place of the items I sent to it in the constructor, and effectively overrids the activity items because they both point to the same objects in memory.
And I realized that by filtering in order to display a subset of results, I am overriding the acitivy's list of items, because it is shared by the activity and the RecyclerView
However, of all the code samples and tutorials I've seen on RecyclerView, I've never seen anyone do a deep copy of the list of items and pass it to the RV, so what am I missing here?
I found the answer in the comments below the accepted answer here:
How to filter a RecyclerView with a SearchView
To quote:
#LalitThapa Just look at the constructor of my ExampleAdapter above. The second line should be interesting for you because that's where I make a copy of the List passed into the Adapter. What you want to have is two separate Lists. One in your Activity or Fragment which contains all items and a second List in your Adapter which contains only the filtered icons. Your problem is that you use the same List instance for both. – Xaver Kapeller Jan 17 at 19:05
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);
I am using merge adapter to merge two custom cursor adapters in android.I could not find clicked section to get data from my custom cursor adapter.How can I get adapter object when listview clicked?.
I tried following way in my onitemclick of listview. But it does not print "clicked on mention question section" text.But it returns android.content.ContentResolver$CursorWrapperInner#41b9a638.How can I find which section is clicked in listview?
if (parent.getAdapter().getItem(position) instanceof FeedMentionQuestionAdapter) {
LivLog.info(getClass(), "clicked on mention question section ");
}
How can I get adapter object when listview clicked?
You need to hold onto your own adapter, or call getAdapter() on your ListView.
But it returns android.content.ContentResolver$CursorWrapperInner#41b9a638
Presumably, you put a CursorAdapter in the MergeAdapter.
Also, getItem() will never return an adapter. It returns items. If you are trying to determine the adapter that handles a particular position, call getAdapter().
How can I find which section is clicked in listview?
If you are referring to SectionIndexer, then sections are not clicked.
I have a ListView where I want each item to have an ID number attached to it (not the same as the position number). I was hoping this could be done by setting a tag to each View item in the ListView using setTag() when these Views are being created.
Right now I'm creating the ListView like this:
final ListView listview = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, names);
listview.setAdapter(adapter);
The names variable in the ArrayAdapter parameters above is an ArrayList, and each string value in this list also has a unique ID that I want to link to this string somehow.
Is there any way I can get access to and modify each of the Views with a tag? One idea was to create my own extended class of ArrayAdapter and override the getView() method, but I don't really understand how it works and how I would go about doing this.
Or is there a better way to link IDs with each string like this than adding tags like I'm trying to do?
Create a ViewBinder and set the tags as the ListView is being populated with whatever you need. You can check all properties of the view to determine what tag goes where, so this should be what you're looking for.
myAdapter.setViewBinder(new MyViewBinder());
public class MyViewBinder implements ViewBinder {
#Override
public boolean setViewValue(View view, Object data, String text){
//Since it iterates through all the views of the item, change accordingly
if(view instanceof TextView){
((TextView)view).setTag("whatever you want");
}
}
}
I just used this exact same answer on another question (albeit slightly different) yesterday.
about getView , it works by using a method of recycling views. i will try to explain it in a simple way.
suppose you have tons of items that can be viewed . you don't want to really create tons of views too , since that would take a lot of memory . google thought of it and provide you the means to update only the views that need to be shown at any specific time.
so , if there is an empty space on the listview , it will be filled with a new view . if the user scrolls , the view that becomes hidden is recycled and given back to you on the getView , to be updated with the data of the one that is shown instead .
for example , if you scroll down , the upper view becomes hidden for the end user , but in fact it becomes the exact same view that is on the bottom .
in order to understand how to make the listview have the best performance and see in practice how and why it works as i've talked about , watch this video:
http://www.youtube.com/watch?v=wDBM6wVEO70
as for tags , i think you want to do something else , since the data itself (usually some sort of collection, like an arrayList) already knows where to update , because you get the position via the getView . if you want a specific view to update , you might be able to do so by using a hashmap that keeps upadting , which its key is the position in the collection , and the value is the associated view . on each time you go to getView , you need to remove the entry that belong to the view (if exists) and assign the new position with the view that you got/created .
Thanks for the answers. thisMayhem's answer would probably have been easier in the end, but on my quest to learn more I ended up making my own adapter according to this tutorial. I pass down the names and the IDs into the adapter and set the names as the text of the TextViews and the IDs as the tags.
I would rather go with the solution discussed in this thread. It is always the easiest to have all related data in same place and in this case you just create a class to hold all the information you will need for every item.