问题
I am trying to load items to the spinner inside my MainActivity
toolbar.Inside MainActivity
there is a fragment called HomeFragment
. And that fragment contains SlidingTabLayout
and once I click different tabs I need to load different data sets to the above mentioned spinner.
Here is how it looks like :
I have implemented method inside MainActivity
to add data to the spinner :
public void addItemsToSpinner(final Collection<String> collection) {
ArrayList<String> list = new ArrayList<>();
list.addAll(collection);
CustomSpinnerAdapter spinAdapter = new CustomSpinnerAdapter(getApplicationContext(), list);
subCategories_spinner.setAdapter(spinAdapter);
subCategories_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapter, View v, int position, long id) {
// On selecting a spinner item
String item = adapter.getItemAtPosition(position).toString();
// Showing selected spinner item
// Toast.makeText(getApplicationContext(), "Selected : " + item,
// Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
And inside my HomeActivity
I have set OnPageChangeLister
to SlidingTabLayout
like below :
mTabs.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if (firstScrolled) {
// Here log-cat gives me I/Position: On page scrolled - 0 which means on stating app this line executed. But data not set to the spinner.
// Log.i("Position", "On page scrolled - " + position);
volleySubCatFilter("women");
((MainActivity) getActivity()).addItemsToSpinner(subs);//debug console subs (ArrayList) size given as 0 here
firstScrolled = false;
}
}
@Override
public void onPageSelected(int position) {
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);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
With use of volleySubCatFilter("category_here");
method I retrieve data from volley request to the server and it is working fine.
The problem I have is first time loading the application WOMEN tab is selected and but no data loaded to the spinner.But I tried with Log.i("Position", "On page scrolled - " + position);
and as soon as the app loaded it gives me I/Position: On page scrolled - 0
in my log-cat.But data doesnt load.
Debuging line ((MainActivity) getActivity()).addItemsToSpinner(subs)
tells 0 items.Once I press another tab then it starts loading data which belongs to previous tab. Example :- Pressing MEN tab right after app loaded getting data belongs to WOMEN tab.Then I click on any tab it start loading data belongs to WOMEN tab.
If I brief this problem data loading to the spinner is one step behind.
It would be grateful if anyone suggest me a way to get rid of this bug.Thanks in advance.
回答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 = new JsonObjectRequest(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.
回答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);
}
回答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 (object instanceof ManFragment) {
ManFragment f = (ManFragment) object;
f.update();
} else if (object instanceof WomanFragment) {
WomanFragment f = (WomanFragment) object;
f.update();
}
else if (object instanceof HouseFragment) {
HouseFragment f = (HouseFragment) object;
f.update();
}
return super.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();
}
来源:https://stackoverflow.com/questions/35692531/regarding-loading-items-to-the-spinner-using-volley-library