greeting,I am developing one app where i am implementing load more on RecyclerView view by using interface.
But the problem is that it work fine for first time and after that mOnLoadMoreListener will be null,i dont know why mOnLoadMoreListener will null.
so it will not load next element
This is my code and my listener is as follows
OnLoadMoreListener.java
public interface OnLoadMoreListener {
void onLoadMore();
}
The main problem is that at first time it load the data well. and when i scroll the list down and when i reach at last item it will load next data at first time but after that it doesnt work...
I checked mOnLoadMoreListener it become null.
So please give a solution. i am getting irritated because of it.
Please help me.Solve this problem because it is important for me.i searched on internet but i didnt get any help
Thanks in advance
As I see, first time you create adapter = new DataAdapter(); in onCreate() method and set adapter.setOnLoadMoreListener(new OnLoadMoreListener()). But later in getProducts() method you recreate adapter (check JsonArrayRequest->onResponse) and lose OnLoadMoreListener. So this is the problem I suppose.
Related
I'm new to all of this and was making good progress but I have hit a brick wall with working out how to do the next thing. I am using Kotlin and have a Fragment with an associated Recyclerview Adapter. I would like to set OnClick (On or Off) against items in a row depending upon a value in the Fragment which could change at any time.
My adapter works fine to show and update the array of data and also to implement OnClick.
I have tried sending a data element via a constructor which changed in the fragment but always showed as the initial setting in the adapter. The same with trying to call a method.
Many other questions touch on the issue but only show snippets of code, and it seems that I'm not advanced enough to get them working successfully in my code.
Could anyone please provide a pointer to a working set of Kotlin code that includes parsing a variable from fragment to adapter - perhaps in Git or a tutorial. I'm sure that if I can study a working program I can move forward. Thank you.
It would have been better if you had included your Adapter and Fragment code in the question, that would have helped us in understanding how you have setup everything and what data model are you passing to adapter.
But looking at your question, one solution that comes to my mind is to add an enabled boolean in your data model that is displayed in the ViewHolder. Using this you can set view.clickable = model.enabled. Now whenever your "value in the Fragment" changes you can update this list and let the adapter rebind items.
Note that the above solution is when you want to selectively enable/disable clicks on individual items. If you want to do this for all items at once, it's better to create a variable in adapter that you can change from the Fragment, and inside the clickListener you can check the value of that adapter variable. If it's false, just return out of the click listener. Something like,
view.setOnClickListener {
if(adapterValue) {
// handle Click
}
}
If this approach doesn't help, I would ask you to add more context in your question and show what you have done so far.
I searched for hours but still have no explanation (even not in official docu) what is the right way to use a RecyclerView.Adapters' notify-methods. Already read a lot of similar question on SO, but no could solve my simple problem. I try to put down a short, simplified example.
class MyAdapterClass extends RecyclerView.Adapter<MyAdapterClass.MyViewHolderClass> {
...
List myItemList = new ArrayList<MyItemType>();
...
}
RecyclerView myRecView = (RecyclerView) findViewById(R.id.myRecView);
myRecView.setAdapter(new MyAdapterClass(...)));
Even when inserting the first item, the view is not refreshed.
myAdapter.myItemList.add(newItem);
myAdapter.notifyItemInserted(myAdapter.myItemList.size() - 1);
If I then tightly touch (try to scroll) the 1-item-listview, the item appears. So it seams to be a refreshing issue. But why? I have an emtpy list, then I insert one item, then I notify the adapter about the insertion. Whats wrong with my expectation?
By the way, even if I additionally call:
myAdapter.notifyItemChanged(myAdapter.myItemList.size() - 1);
it gets not automatically refreshed.
EDIT: And of course, I dont want to use notifyDataSetChanged() :-) only insert, update, remove on an existing list.
Try to add this code inside your adapter class :
#Override
public int getItemCount() {
return myItemList.size();
}
i hope you define in adpter getItemCount() in list size. then used blow code for notify adapter.
myadapter.notifyItemRangeChanged(0, userDataArrayList.size());
I'm starting learning Android and I want to know if there is some option in Android that let you modify each item or view (I don't know how it's named exactly, I mean each of the items from an ArrayList that you show in a ListView).
Well, I made a ListView that is going to show some books that were located in an ArrayList named "books".
I made a custom adapter that I associate to the ListView to show each item with the corresponding layout in the application. I also have a class "book" for each item that is going to be shown in the ListView.
Further, I made an Intent that I call from MainActivity with startForActivityResult(), that I process in Book class and that I return to MainActivity with all data of a book with the method setResult and got the information with onActivityResult() and the requestCode.
So I don't have any problem to add items to the ListView, just I have the problems if I want to modify some of the items (or views) that are located in the ListView (for example if I have title and author of a book, if I put some wrong information, I want have the option to change it).
I have that, in the same moment that you click on some of the items of the ListView, a new layout will be show to modify the information that it's wrong so I use the method setOnItemClickListener with onItemClick event on the custom adapter that I created before. Here it's where I call the new Intent to modify the wrong information with the method startActivityForResult().
I made the same as before to add a new item but, instead of add a new item with custom_adapter.addBook(title,author) I want to know if there is some option to made something like this: custom_adapter.modify(title,author) or custom_adapter.update(title,author), I mean, when you have modify all the items that were wrong of a book (for example an EditText that were "title") and you have all the information in the MainActivity class (because you returned it with setResult), how to put it again in the same item updating it in the custom_adapter and also in the ArrayList.
I searched it on the Internet but I didn't find anything.
I'm sorry if I have a poor English, but I expect that it can be understand.
Thank you very much!
If I am understanding your problem correctly - you could simply modify the ArrayList of type Book that is backing your ArrayAdapter.
So if you know what Book object you want to modify then you can simply make your changes to the Book object itself. As long as this Book is a reference to the same object that you originally added to the ArrayList you instantiated your ArrayAdapter with then you can then call custom_adapter.notifyDataSetChanged() to tell the adapter to redraw its childviews with the new data.
There are some good code samples on the Internet but you have to understand the code for your purposes. So...here is a start, look at Using an ArrayAdapter with ListView. The code shows the use of ArrayAdapter with getView() method. And I hope it shows how to define the listeners, which you need.
How about that for a start? Have fun...
Thank you very much for all help you gave to me. I'm very pleased with you. :)
Finally, I just send the info with a Bundle when I started the Intent, also with the position. And after, I just used this position to set the new info to my items (in my case, books).
Again, thank you very much ;)
I am really desperate, please help me.
In my main Activity I want to remove all items from the listview then add other data. I am able to delete the data and refresh the listview, but when I add new data, something strange happens. I insert the data in my Array. I call notifydatasetchanged on my adapter after every insertion. The adapter gets those items (I have confirmed this in debug mode). But the listview will not refresh. And now comes the VERY STRANGE thing: When I tap the back button on my device, the application quits (this part is ok), but for a moment before quitting it refreshes the listview with the correct data.
Here are some parts of my code:
For deleting the data I use:
newsArrayList = new ArrayList<NewsClass>();
Adapter = new NewsAdapter(this, newsArrayList);
lv.setAdapter(Adapter);
"lv" is my listview. The listview correctly loses all items and refreshes. I have also tried this:
newsArrayList.clear();
Adapter.notifyDataSetChanged();
Also worked like expected. Then I use a "for" loop to add new items to my array, where I have the following lines:
newsArrayList.add(news[i]);
Adapter.notifyDataSetChanged();
As I wrote, when running in debug mode, everything seems fine. I have spent all day working on this problem. I have read several posts about similar issues on stackoverflow, I even watched a video on youtube. I am calling everything from the main UI thread.
I can work around this problem by restarting my activity, but I want to do it better than that. I have tried invalidate and such things, but nothing helped. Please note the VERY STRANGE thing, I wrote...
I think, that I have to force the activity to redraw its views, or something like that, but even refreshDrawableState(); on my listview did not help.
Thank you in advance!
Basically If you want to clear list data then just call
newsArrayList.clear();
lv.setAdapter(null);
Again add what ever your data in ArrayList and set adapter
newsArrayList = new ArrayList<NewsClass>();
Adapter = new NewsAdapter(this, newsArrayList); // considering list with some elements
lv.setAdapter(Adapter);
If you are getting outofmemory error/exception then there is problem with your Adapter Class inside getView method
I have implmented pagination and it display 5 records per page. Now suppose I am on page 3 and click 3'rd element then 3'rd element of page-1 is selected.
I am not able to figure out problem as I always create new list object while setting data.
I used below code
temp = new ArrayList();
this.someListAdapter = new SomeListAdapter(this, R.layout.row_facet,temp);
setListAdapter(this.someListAdapter );
Below is signature of SomeListAdapter class.
public class SomeListAdapter extends ArrayAdapter<VoNeighborhood> {
}
Please help....
There really aren't enough details here about how you do pagination with your ListView.
So I might guess you're overriding onListItemClick and using the position variable it sends you, but you then don't take into account the page you're on?
Alternatively, just don't use pagination as you have an infinite canvas to scroll your list within — I don't think I've recall seeing an Android app so far that uses pagination!