ListView ArrayAdapter reverse of notifyDataSetChanged - android

notifyDataSetChanged updates the view/adapter when you change the object.
But how to have the object updated when the view is changed by the user (Checkbox checked)
I am trying to put the Checkbox onclick listener in the newView(...) and my adapter is in separate file.
My worry is that if I update the array object only internally then when the notifidatasetchanged is called the update will be lost. Is it true? Can I update the main object found in the Activity from the newView() found in the adapter in different file?

When the CheckBox is checked/unchecked, the onClick method for that CheckBox will be called. When that happens, you can change the object in the backing data for your adapter depending on whether the CheckBox is checked or not.
Please, go over the official tutorial: http://developer.android.com/guide/topics/ui/controls/checkbox.html

Related

How to change the format of a recyclerview row due to change in livedata?

I have a standard app setup (Activity, ViewModel, Repository). The Activity contains a RecyclerView. The RecyclerView has an adapter. In my Activity I update the RecyclerView by observing a LiveData object:
mQuizViewModel.getCurrentQuizTask().observe(this, new Observer<QuizTask>() {
#Override
public void onChanged(QuizTask quizTask){
adapter.setAnswers(quizTask.getAnswers());
}
});
If the user hits any item of my RecyclerView the quizTask will be changed and will fire in turn a onChanged event. Depending on which item the user has hit I want to change the font color of a few items of the RecyclerView. To do so I need a reference to each row from the above mentioned observe method within the Activity. How can I solve this problem?
I solved the problem quite easily. I change the color in the onBindViewHolder method of the adapter. I added a field in the class of the object which is passed to the adapter via
adapter.setData(DataToPass)
In the onBindViewHolder method I check with the additional information wrapped in DataToPass if conditions are fullfilled to change the text color.

Android ListView Some Questions

I have some questions about ListView. The Post that I've searched doesn't satisfy me.
If I have five list items and call notifyDataSetChanged() method in
customAdapter, How many times does getView method called?
I have a checkbox on each list and it must be shown only when the delete button is clicked. The Delete button is created on the Activity Class, and the checkbox is created in the Adapter Class (I mean findViewById). Then how can I handle this?
Currently my code changes flag value on Activity Class and call
notifyDataSetChanged() method on Adapter Class.
How can I handle UI without call notifyDataSetChanged() method?
(I've implemented in this way...)
Sorry about not posting my code.
Get view calls every time when item appears at the screen, so in your case it is 5 times.
You can do something like that:
2.1 Create a public method in your adapter, for example:
public void setIsDeleteModeEnabled(boolean isEnabled) {
//Logic here
}
2.2 In your Activity, when Button is clicked call adapter.setDeleteModeEnabled(true);
3 You should call notifyDataSetChanged() only when dataset is changed. For handling UI events you should:
In list item: you should set onClickListeners in getView();
In Activity : onCreate() method in adapter and call it in Activity's onClicks

How to go in getView of BaseAdapter to refresh list item? NotifyDataSetChanged not working

When i call notifyDataSetChanged() compiler goes into getCount() but not into getView method.
I am not adding the new list item, i have just changed some boolean in my existing list item object which i want to use into the if statement in getView method for doing some item-layout changes, but compiler never goes in getView even if i called notifyDataSetChanged()
for debug i tried to print log of that item boolean into getCount(), and that boolean value is changed!
So my question is: Why compiler not goes into getView method when i do some changes in list item? Any solution for this problem?
I have found the solution. Changing the list item and notifyDataSetChanged() method was not called from main thread.

Android - setListAdapter() + notifyDataSetChanged() clarification

I have a ListFragment whose data is populated by a custom adapter ( a SimpleAdapter in my case). I was experiencing issues with using notifyDataSetChanged() from within my class that extended ListFragment. After a lot of looking around and several (useful) Stack Overflow posts later:
listview not updating with notifydatasetchanged() call
Android ListView not refreshing after notifyDataSetChanged
adapters notifyDataSetChanged does not work
notifyDataSetChanged not working
ListView does not update when calling notifyDataSetChanged from a BaseAdapter
I understand that a loose (and highly un-recommended) workaround would be to re-set your adapter using setListAdapter(). However I am now facing issues with this as well.
The documentation, http://developer.android.com/reference/android/app/ListFragment.html#setListAdapter(android.widget.ListAdapter), mentions that setListAdapter()
Provides the cursor for the list view.
But I still have some questions.
Q1. Does initializing an adapter multiple times using setListAdapter() 'point' to the same adapter instance ?
Q2. What actually happens when a call is made to getListAdapter() and then to notifyDataSetChanged() when an adapter has been set multiple times using setListAdapter() ?
Q3. This question is based on an assumption from Q2- when notifyDataSetChanged() is called when an adapter is set multiple times, which of those adapter instances (this part is the assumption), if they exist' is actually being notified for change ?
I am a beginner with Android and I believe there a quite a few nuances I do not understand.I would be extremely grateful if you could clarify these questions. Also thank you very much for your time.
Q1. Does initializing an adapter multiple times using setListAdapter() 'point' to the
same adapter instance ?
Ans: Initializing the adapter will point only to the last instance that you set using setListAdapter.
Q2. What actually happens when a call is made to getListAdapter() and then to
notifyDataSetChanged() when an adapter has been set multiple times using
setListAdapter() ?
Ans: It doesn't matter how many adapters that you have initialized, only the last instance will be retrieved using the getListAdapter().When you use notifyDataSetChanged() only the last instance wich is retrieved using getListAdapter() will be refreshed i.e. ; the last instance will be reloaded(By calling the getView).
Q3. This question is based on an assumption from Q2- when notifyDataSetChanged() is
called when an adapter is set multiple times, which of those adapter instances (this
part is the assumption), if they exist' is actually being notified for change ?
Ans: The above answer contains the explanation for this.

How android's BaseAdapter notifyDataSetChanged method work?

In the document for the method notifyDataSetChanged of class BaseAdapter noted that "Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself."
Supposed I changed the 3rd element in my string array (array data for the ListView), what "any View reflecting the data set should refresh itself" means ? Does the 3rd view item in my list view be notified ?
Also, how is notifyDataSetChanged() and getView() concerned ?
Supposed I changed the 3rd element in my string array (array data for
the listview), what "any View reflecting the data set should refresh
itself" means ?
It means that any view which shows/is based on/uses that data(the string array in your case) should be invalidated(remeasured, redrawn) in order to show the user the new set of data.
Does the 3rd view item in my list view be notified?
No, the parent ListView will be notified. When you set the adapter on a ListView, an observer(from the ListView) will be set for that adapter. Calling notifyDataSetChanged on the adapter will announce that observer from the ListView that something has happen to the data. At this moment the ListView will recreate the rows to show the new data.
Also, how is notifyDataSetChanged() and getView() concerned ?
I'm not sure I understand what you ask. The getView method of an adapter is used by the ListView to obtain a new row each time this is required. When you call notifyDataSetChanged on the adapter this will trigger the observer in the ListView. As it's time to recreate the list, the ListView will call the getView method of the adapter to show the necessary number of rows(the ones visible on the screen). So each time you call notifyDataSetChanged the getView method will be called for the visible rows.

Categories

Resources