I have a fragment with a recyclerview. Here, I'm using a custom method to update the adapter with new dataset.
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter .ViewHolder>{
.....
public void addNewDataset(ArrayList<Integer> data) {
......
notifyDataSetChanged();
}
}
I have called the method like the following, in my Fragment.
myRecyclerAdapter.addNewDataset(data);
Please help me, Thank you in advance.
Did you try to cast recyclerView adpter than call method like this
((MyRecyclerAdapter)recyclerView.getAdapter()).addNewDataset(data);
Compiler search addNewDataset() method in default adapter
Make public method in adapter.
public void addNewDataset(ArrayList<Integer> data) {
......
notifyDataSetChanged();
}
Now make object of adapter in fragment like:
MyAdapter adapter = new MyAdapter(); //make object of adpater like this
adapter.addNewDartaset(data);
Before calling this addnewDataset please check data object reference is not having null value.
if(data!=null){
myRecyclerAdapter.addNewDataset(data);
}
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter .ViewHolder>{
private ArrayList<Integer> mData;
.....
public void addNewDataset(ArrayList<Integer> data) {
if(mData==null){
mData = new ArrayList<Integer>();
}
//If in case you are passing all new array list of integers
mData = data;
//If you want to add new one data to existing array list
mData.addAll(data);
//Use one condition of code from above on the basis of your requirement.
......
notifyDataSetChanged();
}
}
Related
Actually, I have a Recyclerview where has a button and(where I get id with position[from RestAPi call])--->>when the button is clicked I set another recylerview..and now I want to the from the first RecyclerviewAdapter.
I have already tried global variable
Here is images enter image description here
From my previous answer from another question i think you need a Singleton Pattern rather than a Global Variable
You simply need a getter that returns the another Adapter's ArrayList<SingleItemModel> but the problem you will face is that you need to have the same instance of the Adapter from the Activity in order to get the populated ArrayList<Model>.
A good workaround is to used Bill Pugh's Singleton in the Adapter
public class Adapter {
private ArrayList<Model> list;
private Adapter() {}
public static Adapter getInstance() {
return InstInit.INSTANCE;
}
// Don't forget to set the list (or NPE)
// because we can't argue with a Singleton
public void setList(ArrayList<Model> list) {
this.list = list;
}
// You can now get the ArrayList
public ArrayList<Model> getList() {
return list;
}
private static class InstInit {
private static final Adapter INSTANCE = new Adapter();
}
// Some codes removed for brevity
// Overrided RecyclerView.Adapter Methods
.................
}
Retrieving the ArrayList assuming that the following Adapters are Singleton
AdapterOne a1 = AdapterOne.getInstance();
AdapterTwo a2 = AdapterTwo.getInstance();
ArrayList<Model> a1RetrievedList = a1.getList();
// You don't need to create a new instance
// creating a new instance doesn't make sense
// because you need to repopulate the list
// for the new instance.
ArrayList<Model> a2RetrievedList = a2.getList();
// You can also retrieve from AdapterTwo
My AsyncTaskLoader is loading data from a remote server. When new data arrives, naturally a call is made to onLoadFinished. At this point I don't know how to give the new data to the RecyclerView.Adapter. Calling notifyDataSetChanged does not give it the new data: it simply tells it there is new data. So any advice on how I might do this? Right now the best I can think of is to create my own setData method in my implementation of RecyclerView.Adapter as
public void setData(List<MyObject> data){
if(null != data && !data.isEmpty()){
synchronized(mItems){
mItems.clear();
mItems.addAll(data);
}
notifyDataSetChanged();
}
}
Is my idea the best there is? Or is there a more sound way of doing this?
Expose a public method in your adapter to update data.
For example, you could put it like this
public void updateItems(ArrayList<MyObject> myObjects) {
this.data = myObjects;
notifyDataSetChanged();
}
You have two options,
1. Re- instantiate your adapter with your new data and reset the adapter.
2. The way you do it.
I can not think of any other methods.
Ok, I had the same issue and here is the solution what I did.
1st I passed list object from onLoadFinished and in the RecyclerViewAdapter I have created method name setCardInfoList() and there I passed this object to global List object which I define in adapter class.
In onLoadFinished method..
#Override
public void onLoadFinished(android.content.Loader<List<Earthquake>> loader, List<Earthquake> earthquakes) {
if (earthquakes != null && !earthquakes.isEmpty()) {
adapter.setCardInfoList(earthquakes);
adapter.notifyDataSetChanged();
}else {
emptyView.setText("No Earthquake Found...");
}
}
Inside Adapter class
public void setCardInfoList(List<Earthquake> earthquakes){
this.earthquakeList = earthquakes;
}
I have a custom array adapter than extends ArrayAdapter, but the notifyDataSetChanged() method just plain doesn't show up(and does not work if you try it).
class matchAdapter extends ArrayAdapter<Match> {
public matchAdapter(Context context, List<Match> matches) {
super(context, R.layout.match_layout, matches);
}
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
The above is from my adapter class. Would notifyAll() do the same thing?
Your listAdapter is declared as having the type ListAdapter, which doesn't have the method you expect (notifyDataSetChanged). You need to make it at least BaseAdapter or cast it to a BaseAdapter(I am assuming your MatchAdapter is at least a BaseAdapter - if not, well, calling such a method would make no sense:-) )
((BaseAdapter)listAdapter).notifyDataSetChanged();
I'll write an example, it's not a real code but it's how you have to do this.
Create your List
ArrayList<matchList> matchList = new ArrayList<matchList>();
Create an Adapter
adapter = new matchAdapter(this, matchList);
Set the Adapter
YourListView.setAdapter(adapter);
You change the stuff on your List
matchList.add(Stuff for your list);
Notify now that List has changed doing :
adapter.notifyDataSetChanged();
Hope it helps to you, if it does not, just type a comment and I'll try to update my answer.
Edit
I'll give you some examples...
When you want to call notifyDataSetChanged() just do this :
List.clear();
List.addAll(YourStuff);
adapter.notifyDataSetChanged();
Other method could be, you are using an AsyncTask so your onBackGround() you could make it returns items for your list, then on your onPostExecute update the adapter.
Example :
#Override
protected List<matchList> doInBackground(String... params) {
List<matchList> items = loadUpdatedDataset(params);
return items;
}
#Override
protected void onPostExecute(List<matchList> itemschangeds) {
updateAdapterDataset(itemschangeds);
adapter.notifyDataSetChanged();
}
Have you tried compiling and running the app? If there are any syntax errors above your matchAdapter (like a missing ; or "), it'll screw the autocompletion up.
I have an ArrayList, which is defined in one function, and gets called in another one. But when I add public ArrayList<String> list = new ArrayList<String>(); at the beginning, then run a function that adds items to that list and then try to call list in another function, it overwrites the previous set and just returns an empty list.
But when I just have public ArrayList<String> list; at the beginning, again add items in one function and then try to use list, it throws a NullPointerException.
I also tried having ArrayList<String> list = new ArrayList<String>(); in the onCreate method, but that's a NPE too.
So one function is just
public void setList() {
for(int x=0; x<=5;x++){
list.add(Integer.toString(x));
}
}
(the Integer.toString(x) is currently just for testing)
And the Other one is
public void setList() {
Log.d("Log",list.toString());
}
At the very beginning after public class MainActivity extends FragmentActivity {
I have tried
public ArrayList<String> list; //this throws a NPE
and
public ArrayList<String> list = new ArrayList<String>(); //this overwrites the variable each time it's called
How do I fix that?
Could the issue be that I change the displayed Fragment after the set method and before the get method is called?
Thank you very much
Use 1 list object at the top of the code and do not create new ones in the functions. It should look like this.
public static ArrayList<String> list = new ArrayList<String>();
// onCreate....
public void addElement() {
for(int x=0; x<=5;x++){
list.add(Integer.toString(x));
}
}
public void showElements() {
Log.d("Log",list.toString());
}
How i can access ArrayList from one Activity to another and also clear ArrayList value?
you can use setter/getter method for it.
public class MySetGet
{
private ArrayList aList = null;
public void setList ( ArrayList aList )
{
this.aList = a.List;
}
public ArrayList getList ()
{
return aList;
}
}
Now you can set its value from any Activity/Class and get its value from any Activity/Class.
The most simply and possible way to do the desired::
1.Create a simple public class say Data..
Now create a public static your Array list object.
Now access any where..
Data.listObj
2.Create the List object as public static in one activity and use in another via,
SecondActivity.listObj.clear();