how do I get the list of data that was populated by the volley request because the fragment is getting an empty list which has been instantiated in the main activity.
Main Activity
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private ViewPager pager;
private ViewPageAdapter adapter;
private SlidingTabLayout tabs;
private int numTabs = 2;
private ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendJsonRequest();
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
adapter = new ViewPageAdapter(getSupportFragmentManager(),titles,numTabs);
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
tabs.setDistributeEvenly(true);
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
//return getResources().getColor(R.color.tabsScrollColor);
return ContextCompat.getColor(getApplicationContext(), R.color.cgred);
}
});
tabs.setViewPager(pager);
////////////////////////////////////////
// Toast.makeText(getApplicationContext(),getList().toString(),Toast.LENGTH_LONG).show();
}
This method makes the request for the web service data and populates the array with method parseJson
public void sendJsonRequest() {
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, BCWATCH, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
parseJson(response); // This method populates the list instantiated at the beginning of the main activity
progressDialog.dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),"Error!",Toast.LENGTH_LONG).show();
progressDialog.dismiss();
}
});
queue.add(jsonObjectRequest);
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Getting data...");
progressDialog.show();
}
The Fragment
My fragment receives an empty list, I believe it is because of the asynchronous execution of the request, but how to get the full list in the fragment?
public class Tab1 extends android.support.v4.app.Fragment {
HashMap<String, String> obj;
ArrayList<HashMap<String, String>> list;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
list = ((MainActivity) getActivity() ).getList();
if(list != null){
//Toast.makeText(getContext(),"Not null",Toast.LENGTH_LONG).show();
for (HashMap<String, String> entry : list) {
Set<String> keys = entry.keySet();
for (String key : keys) {
String value = entry.get(key);
Toast.makeText(getContext(),"key:"+key+" value:"+value,Toast.LENGTH_LONG).show();
}
}
}
}