Update ViewHolder values of custom Adapter for Recyclerview - android

I have a RecyclerView with a custom Adapter to display rectangular elements (about 15, the number can change if the user adds/removes items) in a grid (using Gridlayout Manager).
These elements consist of an ImageView and a TextView. I want to update both Views regularly after receiving new results from network requests (say every 2minutes).
What would be the best way to update these views? How can I identify the specific ViewHolder? How can I access the ViewHolder values of these elements? Would it be best to keep a reference to each ViewHolder in the Activity where I want to change them? Or should I create a new element with the new values and replace the current element with it? What would be considered best practice?

If you are storing your value of image view and text view in an arraylist you can call notifyDataSetChanged on the adapter when you have a change in the dataset.
For example if you have say a Grid object whose attributes include image uri(to set the image) and string(to set in text view) and some Array list say
List<Grid> gridList = new ArrayList<>();
you can update whatever new data you get in the Arraylist and after you are done updating in the array list call
adapter.notifyDataSetChanged();

The best practice is update views and it's data when some data change. You have to do this always. The method
adapter.notifyDataSetChanged
do this for you.
When you update some data you have to tell to the system, because there's an important action named recycle, when you use a view holder you giving the possibility to the system recycle a view, what means views that don't change the date are stored on the view holder, so the list doesn't need to use the
findViewById
again.

Related

Items with dynamic content in a listview

I'm pretty new to Android and I have a question about an application that I'm trying to develop.
My problem is about listview and adapter: I have a list of objects that contain information about a user's favorite routes (i created an object FavouriteRoute and i pass an ArrayList to the BaseAdapter). However, each object can contain a variable number of cities, of stops and days of the week ... how do I handle this dynamic content and show it inside of each row of listview? What's the best approach? I have to manage these elements within the getView () of the adapter and insert them dynamically within the layout of each row?
You can take the reference of these tutorials to learn creating your adapter which will server your purpose.
http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=92
https://www.caveofprogramming.com/guest-posts/custom-listview-with-imageview-and-textview-in-android.html
http://www.androidhive.info/2014/07/android-custom-listview-with-image-and-text-using-volley/
If you want that some of your views are visible and some are not then in getView() do this:
if(your condition)
textView.setVisibility(View.GONE);
after inflating the views.

Android ListView real time content

I have a simple listView with simple adapter and I am getting a new live content (it means data with changed items) every 5 sec. I have to update some rows with new values, so I clear adapter and addAll in list, but then all list is freezing and scrolling up.
If it's a short list, all is work good, the problem becomes when I have long list with scroll.
Is there better solution for my task? How could I monitor only changed rows and redraw there?
Instead of ListView use RecyclerView. RecyclerView allows to refresh and add individual elements to list.
Adapter method item added at position
So my solution is. As I have abnormal list (I'm getting response from back-end with different values for each row, I must to group it and generate unique layout for each row), so I've used a TableLayout, I generated each row and put it to Map, when I catch new response I check old value with new one and if there are different, I take View from Map and bind new data.

Android list view to display different data sets

Is there a way to get the listview to display data provided from another array list without having to create a new adapter and setting it?
I ask because the way I have it now prevents me from maintaining the scroll position of the list view
I assume you are talking about different datasets and different display per dataset.
If this is the case, you can use one of few solutions:
feel free to use either RecyclerView with multiple ViewHolder classes for it.
Try unifying the dataset representations with a common parent class and use one Adapter while overriding the getViewTypeCount to return 2 and then inflate different views for each type.
Try using MergeAdapter.

Complex Items in a Listview

I have a ListView where the view for each item is a string (the name of the item). But I have to associate a lot of other data with that item: price, size, weight, tax, etc. So, I'm of creating a new Java class called Item, and then an object for each item in the list.
I don't know which is the bext way to implement this. There's two obvious choices:
1) I can simply create the data structure outside of any Android Views, and then write a method called UpdateList() which takes the name of each item in this data structure and puts it in the ListView. The problem with this is that some of the data is duplicated twice (the original data structure, and the adapter for the ListView) and when you duplicate data, bug potential arises.
2) Or, I can somehow associate the data structure directly with the adapter for the ListView and have it figure out how to display the name for each ListView entry that is displayed. The advantage here is that you only have a single data structure. But I don't know if this is possible in Android, or very complex.
Which is the preferred way to do this with Android apps?
You would be better with the ListView and the Adapter option, You would need to create a custom ArrayAdapter to populate a ListView from this objects the way you want.
The advantage of this technic is that you gain a Views recycle mechanism that will recycle the Views inside you ListView in order to spend less memory.
In Short you would have to:
1. Create an object that represents your data for a single row.
2. Create an ArrayList of those objects.
3. Create a layout that contains a ListView or add a ListView to you main layout using code.
4. Create a layout of a single row.
5. Create a ViewHolder that will represent the visual aspect of you data row from the stand point of Views.
6. Create a custom ArrayAdapter that will populate the rows according to you needs.
7. Finally assign this ArrayAdapter to your ListView in onCreate.
You can get an Idea of how to implement this by reading this blog post I wrote:
Create a Custom ArrayAdapter
Just use the adapter. It's much cleaner. Then you can retrieve the info you need when you display the list item with getView(). See this example.

How to reuse one custom listview activity in another activity?

Can I use my existing ListView as a sub-view in another view.
eg. I have a custom view, players list, I have implemented with a ListActivity and ArrayAdapter and is working fine.
Now I want a way to get this listview as View object so that I can add this view object as a child to another view.
I am thinking like, to build entire listview: my ListActivity is calling the ArrayAdapter iteratively by passing ArrayList item each time to build a list item view.
If I am correct, I need a way to call the same ArrayAdapter and need to prepare a ListView Object, right?
Can anybody plz help me.
Thanks in advance.
vpapana
The ListView and the Adapter are two different things:
The data is somewhere (in an array or if you need to be able to add/remove items, in an ArrayList)
The Adapter contains the data
The ListView displays it.
So:
Construct your empty ArrayList
Construct your adapter based on your ArrayList
Construct your ListView based on the Adapter
Then:
Each time you need to change the data, update the ArrayList
Call Adapter.notifyDataSetChanged()
Watch your list being automatically updated :)
To add your list to a tablelayout, see the API documentation, SDK tutorial and this tutorial which has a specific section on how to add rows programmatically.

Categories

Resources