call method from fragment to fragment ( refresh adapter )

boudi

Hello I am trying to call this function loadFiles() which is located in fragmentB and I want to call it from FragmentA, so I can refresh my GridView for my images and videos.

I get this error from the same method

FATAL EXCEPTION: main
    java.lang.NullPointerException

my method in FragmentB:

  public void loadFiles(){
        gridView = (GridView) this.v.findViewById(R.id.grid);
        File f = new File(home);
        if (f.exists()){
            String [] media = f.list();
            ArrayList<String> files = new ArrayList<>();
            for (int i=0; i<media.length;i++){
                if (media[i].endsWith(".jpg")||
                        media[i].endsWith(".png")||
                        media[i].endsWith(".JPEG")||
                        media[i].endsWith(".3gp")||
                        media[i].endsWith(".mp4")||
                        media[i].endsWith(".mov")){
                    files.add(media[i]);
                }

                if (i==media.length-1){
                    GridAdapter adapter = new GridAdapter(this.v.getContext(), files, gridView);
                    adapter.notifyDataSetChanged();
                    gridView.setAdapter(adapter);
                }
            }
        }
    }

How I do call it in FragmentA:

FragmentB b = new FragmentB();
b.loadFiles();

I think the problem is because of this line but I have tried everything and there is no luck :(

GridAdapter adapter = new GridAdapter(this.v.getContext(), files, gridView);
Rustam

I would suggest you to do like this:

 public class Utils{
 public static ArrayList<String> loadFiles(){
         File f = new File(home);
        if (f.exists()){
            String [] media = f.list();
            ArrayList<String> files = new ArrayList<>();
            for (int i=0; i<media.length;i++){
                if (media[i].endsWith(".jpg")||
                        media[i].endsWith(".png")||
                        media[i].endsWith(".JPEG")||
                        media[i].endsWith(".3gp")||
                        media[i].endsWith(".mp4")||
                        media[i].endsWith(".mov")){
                    files.add(media[i]);
                }
            }
            return files;
        }
    }
}

Now in FragmentA and FragmentB etc. you can do

  gridView = (GridView) this.v.findViewById(R.id.grid);
     GridAdapter adapter = new GridAdapter(this.v.getContext(), Utils.loadFiles(), gridView);     
       gridView.setAdapter(adapter);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Call a Fragment method from an Adapter

From Dev

Refresh fragment from adapter

From Dev

failed to call Adapter method from fragment

From Dev

Call Fragment Method from adapter class

From Dev

failed to call Adapter method from fragment

From Dev

Call method from Main Activity into Fragment Adapter

From Dev

call a method in a fragment, from a class that extends from RecyclerView.Adapter

From Dev

Call element from fragment in Adapter

From Dev

Call element from fragment in Adapter

From Dev

How to call fragment from adapter

From Dev

Call method in fragment from another fragment/activity

From Dev

Call method in fragment from another fragment/activity

From Dev

Android Call Fragment Method from Fragment

From Java

Call an activity method from a fragment

From Dev

Call Fragment method from Activity

From Dev

Call Fragment method from Activity

From Dev

Call SQLiteOpenHelper method from fragment

From Dev

Call method in MainActivty from Fragment

From Dev

Access fragment from adapter

From Dev

how to call method of one fragment from another fragment class in android

From Dev

Call method in child fragment in parent fragment from activity

From Dev

Call method in child fragment in parent fragment from activity

From Dev

Call method of fragment B from Fragment A using Kotlin

From Dev

Call dynamic fragment method from activity

From Dev

Call Tabbed Fragment method from Activity

From Dev

How to call method inside the fragment from activity?

From Dev

Call fragment method from activity but view is null

From Dev

Android - Trying to call parent method from fragment

From Dev

(Android) how to call method in fragment from fragmentActivity

Related Related

HotTag

Archive