Regarding Loading Items To The Spinner Using Volley Library
Solution 1:
You need to provide the sequential steps as your data is being loaded in the background thread
. When you select a page, you have two options.
1. Initiate either a Synchronous Volley call
and update the spinner adapter after this call
RequestFuture<JSONObject> future = RequestFuture.newFuture();
JsonObjectRequest request = newJsonObjectRequest(URL, null, future, future);
requestQueue.add(request);
try {
JSONObject response = future.get(); // this will block (forever)ArrayList<String> some_items = response.getStringArray();
//update the spinner with new items
} catch (InterruptedException e) {
// exception handling
} catch (ExecutionException e) {
// exception handling
}
2. Register a BroadcastReceiver
in your Activity
and then sendBroadcast
through LocalBroadcastManager
on VolleyResponse
. and update the spinner items in onReceive of BroadcastReceiver
Hope this helps.
Solution 2:
I am not sure it's a bug or feature that initially pagechangelistener is not being called in case of app start-up. It gets called once you switch the tab. I would like you to make some changes in the code:
Write the functionality you wish to perform in a method in the Activity and then call this in the onPageSelected method.
mTabs.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int index) {
if (position == 0) {
// Log.i("Position", "On page selected - " + position);volleySubCatFilter("women");
((MainActivity) getActivity()).addItemsToSpinner(subs);
}
if (position == 1) {
// Log.i("Position", "On page selected - " + position);volleySubCatFilter("men");
((MainActivity) getActivity()).addItemsToSpinner(subs);
}
if (position == 2) {
// Log.i("Position", "On page selected - " + position);volleySubCatFilter("household%20goods");
((MainActivity) getActivity()).addItemsToSpinner(subs);
}
}
...
}
And then right after calling
setCurrentItem(index);
in the Activity, add the following if statement
if(index == 0) {
volleySubCatFilter("women");
((MainActivity) getActivity()).addItemsToSpinner(subs);
}
Solution 3:
Please go through following steps :
- Create an interface :
public interface Updateable {
public void update();
}
implement above interface in your ViewPager Fragments.
In your PagerAdapter class, override the
getItemPosition(Object object)
`public int getItemPosition(Object object) {
if (objectinstanceofManFragment) {
ManFragment f = (ManFragment) object;
f.update();
} elseif (objectinstanceofWomanFragment) {
WomanFragment f = (WomanFragment) object;
f.update();
}
elseif (objectinstanceofHouseFragment) {
HouseFragment f = (HouseFragment) object;
f.update();
}
returnsuper.getItemPosition(object);
}`
Now in your fragment's update() method, provide the logic for updating the spinner data in toolbar. It can be done by providing the
invalidateOptionMenu()
in update method andsetHasOptionsMenu(true)
in OncreateView of your fragment. Toolbar spinner data updation can be done by implementingonPrepareOptionsMenu(Menu menu)
override method.In you tabs page scroll logic, notify the adapter :
@Override
public void onPageScrollStateChanged(int state) {
yourAdapter.notifyDataSetChanged();
}
Post a Comment for "Regarding Loading Items To The Spinner Using Volley Library"