0

Hello everyone i need how to passs data from activity to fragment if any easy way

I need to implement by using Interface

Here is the example of my data type.

ArrayList<PreviewData> finaldata = new ArrayList<PreviewData>();
for(int i=0;i<10;i++) {
    PreviewData tempdata = new PreviewData("listname"+i+"","dsdsdsd","sdsdsdd");
    finaldata.add(tempdata);
}     

I am using the dummy values for explaing the code in but my case values coming from web api.

Now problem is i have a activity with two tab . tab1 or tab2

when activity start then it fatch the data from server and store it on custom array list. eg. finaldata in my case

now in tab1 here is a list view that get the list from finaldata

Here is the code of onCreateView Of fragment . i need to pass the final data values from main activity to fragment adapterdata list.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mydbhelper = new DBhelper(getContext());
    view = inflater.inflate(R.layout.view_all_campaign, container, false);
    ArrayList<PreviewData> adapterdata= new ArrayList<PreviewData>();
    listView = (ListView) view.findViewById(R.id.campaign_list_view);
    adapter = new MyCustomAdapter(getContext(), R.layout.layout_contact_row, adapterdata);
    listView.setAdapter(adapter);
    return view;
}
Joshua
  • 5,901
  • 2
  • 32
  • 52
sunny
  • 179
  • 2
  • 14

3 Answers3

1

Create a Constant class like below

public Constant{

    public static ArrayList<Previewdata> data=null;

}

Then assign array value to the constant class like below

    ArrayList<PreviewData> finaldata = new ArrayList<PreviewData>();
for(int i=0;i<10;i++) {
    PreviewData tempdata = new PreviewData("listname"+i+"","dsdsdsd","sdsdsdd");
    finaldata.add(tempdata);
}   

 Constant.data=finaldata;

And retrive from fragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mydbhelper = new DBhelper(getContext());
    view = inflater.inflate(R.layout.view_all_campaign, container, false);
    ArrayList<PreviewData> adapterdata= Constant.data;
    listView = (ListView) view.findViewById(R.id.campaign_list_view);
    adapter = new MyCustomAdapter(getContext(), R.layout.layout_contact_row, adapterdata);
    listView.setAdapter(adapter);
    return view;
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Andolasoft Inc
  • 1,296
  • 1
  • 7
  • 16
  • Thnx bro i will try and rply you – sunny Aug 01 '16 at 07:10
  • i wl return NullPointerException – sunny Aug 01 '16 at 07:19
  • Constant constant = new Constant(); @Override protected void onPostExecute(String result) { Log.i("OnPostExecution","Task Done"); constant.data = finaldata; } – sunny Aug 01 '16 at 07:34
  • Cause NullPointerException because the data coming from server and when activity started then it's also run thee fragment itself. but the values are null at startup – sunny Aug 01 '16 at 07:45
  • have you checked the finaldata is initialized or not..??before assign to Constant.data.. – Andolasoft Inc Aug 01 '16 at 07:58
  • im using like this public class Constant { public static ArrayList data=null; public static ArrayList getData() { return data; } public static void setData(ArrayList data) { Constant.data = data; } } – sunny Aug 01 '16 at 08:05
  • when you what to get then in fragment ArrayList adapterdata=Constant.getData; – Andolasoft Inc Aug 01 '16 at 08:10
  • No need to create object of Constant class..all static variables..so can call all the variables by using class name. – Andolasoft Inc Aug 01 '16 at 08:11
  • And one thing no need to use set and get in constant class remove those and try only set the data.. first try to the above after than this.. – Andolasoft Inc Aug 01 '16 at 08:13
  • yes bro every thing is working fine but the problem in when activity started it call the api and it will take time to get data and parse it according to the finaldata list. but when is activity is called then the tab layout onCreateView is also created bcuz it's the part of the activity. now in this condition there no data in final data bcuz background task is in progress. adapter = new MyCustomAdapter(getContext(), R.layout.layout_contact_row, adapterdata); adapterdata is null it cause the RuntimeException. – sunny Aug 01 '16 at 08:20
  • then wait till data fetched from server then show the fragment..use a progrees dialog to wait the user.. – Andolasoft Inc Aug 01 '16 at 08:24
  • onething why you not try to get all data in frament ..instead of activity.. – Andolasoft Inc Aug 01 '16 at 08:26
  • thnx for your valueable time . yes im doing it :) – sunny Aug 01 '16 at 08:26
1

Please Add this code to your Activity when you call the fragment:

Bundle bundle = new Bundle();
ArrayList<PreviewData> finaldata = new ArrayList<PreviewData>();
bundle.putSerializable("myArray",(Serializable)finaldata);
// set Arguments
Fragmentclass obj = new Fragmentclass();
obj.setArguments(bundle);

But first make your PreviewData like as follows(name is for example) :

 public class PreviewData implements Serializable{

     private String name;

    public String getName() {
         return name;
    }

    public void setName(String name) {
         this.name = name;
      }
   }

Then get your object in your fragment like :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mydbhelper = new DBhelper(getContext());
view = inflater.inflate(R.layout.view_all_campaign, container, false);

ArrayList<PreviewData> adapterdata= new ArrayList<PreviewData>();
adapterdata = getArguments().getStringArrayList("myArray");
listView = (ListView) view.findViewById(R.id.campaign_list_view);

adapter = new MyCustomAdapter(getContext(), R.layout.layout_contact_row, adapterdata);
listView.setAdapter(adapter);
return view; 
}
Harshad07
  • 608
  • 7
  • 23
0

In activity:

public ArrayList<PreviewData> getAdapterData {
     return finaldata;
}

In fragment:

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ArrayList<PreviewData> adapterdata = ((YourActivity) getActivity()).getAdapterData();
    adapter = new MyCustomAdapter(getContext(), R.layout.layout_contact_row, adapterdata);
    listView.setAdapter(adapter);
}
faranjit
  • 1,567
  • 1
  • 15
  • 22