-1

Hello i am on fragment and want to access the arraylist size i declared the Arraylist as:

 public static ArrayList<Data> mydata;

in The Activity class

and method to return the size is:

 public int returnsize(){
    return mydata.size();
}

and on FRAGMENT I Retrieve it as:

//Myaudio is the class/activity from where i want to access
Myaudio myaudio = new Myaudio();
myaudio.returnsize;

But with no luck..please help me is there a better way...

Amanth Rai
  • 191
  • 2
  • 12
  • You need to pass the ArrayList to the fragment. In the above code, you are creating a new instance. Won't get the prev value. https://stackoverflow.com/a/39868180/2633909 – DKV Dec 31 '18 at 06:13
  • Did you initialized and add anything in the list mydata? Show some more code If the list is static you can get the data by calling ClassName.mydata.size(); – Rakshit Nawani Dec 31 '18 at 06:13
  • yes..mydata contains audio files...please see the edits – Amanth Rai Dec 31 '18 at 06:14
  • @AmanthRai you want to access `mydata` in Fragment?? – Anmol Dec 31 '18 at 06:18
  • i just want the size(). of mydata in fragment – Amanth Rai Dec 31 '18 at 06:19
  • just create a callback and send it while creating Fragment and just do `italkToActivity.getMyDataSize()` – Anmol Dec 31 '18 at 06:22
  • From your code i am not able to get full gist of what you are trying to do share Your full activity and fragment code – Anmol Dec 31 '18 at 06:28

2 Answers2

2

You can use callback's to talk between activity and Fragment's.

Here's how

create a interface eg: ITalkToActivity

public interface ITalkToActivity{
    int getMyDataSize();
}

Implement the above in your activity, your activity should return the size of myData list.

@Override
  public int getMyDataSize(){
  return mydata.size();
}

private startYourFragment(){
 Fragment yourFragment = YourFragment(this)  //setting listener i.e ITalkToActivity for fragment 
 supportFragmentManager.beginTransaction().replace(R.id.root, yourFragment, "YOUR_FRAGMENT").commit()
 supportFragmentManager.executePendingTransactions()
}

Your Fragment Constructor should save the instance of ITalkToActivity in a global variable and access it wherever needed.

public class YourFragment extends Fragment{

private ITalkToActivity iTalkToActivity;

 YourFragment(ITalkToActivity iTalkToActivity){
   this.iTalkToActivity=iTalkToActivity
 }

  private void yourFunction(){
    int dataSize = iTalkToActivity.getMyDatatSize();
  }

}
Anmol
  • 8,110
  • 9
  • 38
  • 63
  • I created a interface like you mentioned I got two activities where should i save the instance of the interface.. – Amanth Rai Dec 31 '18 at 06:52
  • create interface separately in a java file and implement it in the activity you want the size of list from! – Anmol Dec 31 '18 at 06:53
  • Yes it's in a separate java file... And i implemented it on Myaudio activity from where i want the arraylist size(). Now how to notify the fragment for getting the size – Amanth Rai Dec 31 '18 at 06:56
  • check my answer i have explained it just set the instance of interface from where you are creating the Fragment and access it anywhere with the global variable – Anmol Dec 31 '18 at 06:57
  • https://www.geeksforgeeks.org/callback-using-interfaces-java/ read this @AmanthRai if you are not getting how to set up callbback – Anmol Dec 31 '18 at 07:06
-1

you can direct get size of static ArrayList like this

 (ActivityName.mydata != null){

  ActivityName.mydata.size();
   }

here ActivityName is name of Activity where ArrayList declaired.

Mayur Dabhi
  • 3,607
  • 2
  • 14
  • 25
  • but this is not good practice as user can directly manipulate the list from fragment and make code less decoupled. better to use Interface as callbacks – Anmol Dec 31 '18 at 06:33
  • alternate way is you have to pass arraylist in fragment using bundle. – Mayur Dabhi Dec 31 '18 at 06:36
  • no need of that also check my answer as it make's the code decoupled and easy to change the implementations function @MayurDabhi – Anmol Dec 31 '18 at 06:43
  • its possible but if you can pass interface instance in fragment then you should direct pass size of arraylist - @Anmol – Mayur Dabhi Dec 31 '18 at 06:50
  • yes get only what is needed nothing else if you get instance of the arraylist in Fragment then you have to check at two places if some state dispute occur's in arraylist so better is not to get instance of arraylist(or any data) from activity to Fragment as code become to fragile. @MayurDabhi – Anmol Dec 31 '18 at 06:52