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
Related
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.
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
I am trying to display a list fragment in the middle of my activity. The list fragment adapter is a custom adapter (extended from BaseAdapter) with the typical ViewHolder pattern. It is implemented correctly.
I have the adapter set up with greenrobot Eventbus to receive a new List object from an asynctask which does the query in the background (as not to slow down the UI Main Thread).
The problem is the list fragment doesn't have the results of the database query initially so it defaults to empty (and displays the textview in my xml for the main activity which has the id 'empty').
In the end, my adapter, and listviewfragment don't get instaniated at all because it defaults to empty.
Is there a better method to doing this?
How can I get my listview fragment to wait for the data recieved from the asyncTask?
I am going to just do a fragment with a listview in it, instead of a listview fragment, and I'll see if that will help.
As I understood correctly, you did everything okay so far. What you have to do is to add an "update" method to your adapter, and call this whenever your asynctask finished it's job and got the results for you.
This update method should look something like
public void updateAdapterData(List<String> newList) {
mItems.clear(); // say mItems is the global list you have in the adpater
mItems.addAll(newList);
notifyDataSetChanged(); // This will cause the adpater to re-draw it's rows, so now data should be visible in your list
}
And in your fragment you do somehting like:
adpater.updateAdapterData(newList);
Does this help?
I have the following situation:
I have a singleton controller which contains a List<Foo>
when my activity starts I load the items to my BaseExpandableListAdapter
the problem: When I now delete a item form the singleton List it gets net deletet in the view. Only when I start the whole activity again. How to solve this without starting the whole activity again?
public void notifyDataSetChanged ()
Added in API level 1
Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.
http://developer.android.com/reference/android/widget/ArrayAdapter.html#notifyDataSetChanged%28%29
When you change the database that is used by the adapter, you should call notifyDataSetChanged() on the adapter.
My problem is the following : at the moment I have a spinner called "projects" that I am populating in my code, but for some reasons I need to add it a title so I just do it using my adapter :
adapter.add("Choose a project");
Now I would like to remove it (via adapter.remove, this is not a problem) when the spinner in my activity is clicked (when it's opening), but the only method i am able to find is onItemSelected, which is not what I'm looking for.
Is there such thing as a onOpenListener or onClickListener for Spinner to actually execute some code when the spinner is opened, and not only when an item is selected ??
Thank you
This is probably because you are trying to call it inside the listener which is probably an anonymous class. Assuming your activity's class name is MyActivity use MyActivity.this.someactivitymethod where someactivitymethod is the method that you want to execute.
Spinner inherits from AbsSpinner which in turn inherits from AdapterView, which means it inherits the setOnClickListener method from AdapterView and this can be used to perform an action on clicking the spinner.
http://developer.android.com/reference/android/widget/AdapterView.html#setOnClickListener(android.view.View.OnClickListener)
EDIT: While this is true, it throws a runtime exception. Sorry.