FlipView Adapter not Updating - java

The adapter of a flipview is set onCreate. Everything works fine. The user is meant to select some items on another activity which would update the items on the flipview. I implemented this by changing the adapter of the flipView when onResume is called and the dismiss activity is the selectionActivity. The code is executed but it does not reflect on the flipView items.
What i do on onCreate
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View rootView = inflater.inflate(R.layout.flip_main, container, false);
mFlipView = (FlipView) rootView.findViewById(R.id.flip_view);
mAdapter = new FlipAdapter(getActivity(),getHomeItems(), getFragmentManager());
mAdapter.setCallback(this);
mFlipView.setAdapter(mAdapter);
mFlipView.setOnFlipListener(this);
if (isFirstShown) {
mFlipView.peakNext(false);
isFirstShown = false;
}
mFlipView.setOverFlipMode(OverFlipMode.RUBBER_BAND);
mFlipView.setEmptyView(getActivity().findViewById(R.id.empty_view));
mFlipView.setOnOverFlipListener(this);
return rootView;
}
what is called after the user makes selection
FlipAdapterTest mAdapter = new FlipAdapterTest(getActivity(), getHomeItems(), getFragmentManager());
mFlipView.removeAllViews();
mFlipView.setAdapter(mAdapter);

Related

why setVisibility in fragment doesn't work?

the ProgressBar doesn't disappear in the fragment also items aren't loaded in the RecyclerView, When the code was in the body of onCreate of the main activity ,it was working
I am making fragment that contains ProgressBar and RecyclerView with retrieving data from firebase database , I added setVisibility at the end of onDataChange method so that after getting all the data and store it in the array list , the ProgressBar disappear
RecyclerView offersRecycler;
ArrayList<offer> offers;
OffersAdapter offersAdapter;
View fragment_layout;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
fragment_layout = inflater.inflate(R.layout.fragment_offers, container, false);
// Offers RecyclerView
offersRecycler = fragment_layout.findViewById(R.id.offersRecycler);
offers = new ArrayList();
offers_init();
offersAdapter = new OffersAdapter(offers,getContext());
offersRecycler.setAdapter(offersAdapter);
offersRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_offers, container, false);
}
private void offers_init() {
DatabaseReference db_offers = db_ref().child("offers");
db_offers.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
try {
ProgressBar loadingOffers = fragment_layout.findViewById(R.id.loadingOffers);
loadingOffers.setProgress(10);
for (DataSnapshot offer_item : dataSnapshot.getChildren()) {
String id = offer_item.getKey();
String title = offer_item.child("title").getValue().toString();
String rating = offer_item.child("rating").getValue().toString();
String orders_number = offer_item.child("orders_number").getValue().toString();
offer offer_object = new offer(id,title, rating, orders_number);
offers.add(offer_object);
offersAdapter.notifyDataSetChanged();
}
Toast.makeText(getContext(),"title",Toast.LENGTH_LONG).show();
loadingOffers.setVisibility(View.GONE);
}catch (Exception ex){
Toast.makeText(getContext(),ex.getMessage(),Toast.LENGTH_LONG).show();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
});
}
I expect the ProgressBar to hide and the array loaded into the RecyclerView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
fragment_layout = inflater.inflate(R.layout.fragment_offers, container, false);
// Offers RecyclerView
offersRecycler = fragment_layout.findViewById(R.id.offersRecycler);
offers = new ArrayList();
offers_init();
offersAdapter = new OffersAdapter(offers,getContext());
offersRecycler.setAdapter(offersAdapter);
offersRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_offers, container, false);
}
^ This is your code snippet. What you are doing is - inflating a layout fragment_layout = inflater.inflate(R.layout.fragment_offers, container, false) and using fragment_layout to get the instance of RecyclerView. This is fine.
But at the end of the method, you are inflating a new layout and returning that. So the Android framework uses that View instead of fragment_layout to set the Fragment view. All the views in fragment_layout won't be shown since it's never added to the fragment and hence you don't see any changes in the RecyclerView.
At the end of the onCreateView method, you should just return fragment_layout.
in your CreateView, change
return inflater.inflate(R.layout.fragment_offers, container, false);
by
return fragment_layout;
You call the layout, init recyclerview, but recall the "blank" layout
Also, avoid underscore for method or variable name, it's not the java convention

How to show my spinner within one fragment

I am on creating an application and at the moment, I have created two fragments which is going to be a "navigation" sort of thing. On the second of the two fragments, I have put a spinner on which is where the user would be able to select a particular option. All of the spinners have unique ID's but the findViewById method cannot be resolved
All of the strings are in the strings xml file and when I tried this spinner in my testing app, it worked
public class Frag2 extends Fragment implements AdapterView.OnItemSelectedListener{
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.frag2_layout, container, false);
Spinner spinner = findViewById (R.id.spinner1);
ArrayAdapter.createFromResource(getActivity(), R.array.insulin, android.R.layout.simple_spinner_item);
ArrayAdapter<CharSequence> adapter;
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), text, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
The spinner should work within the one fragment and then I would like to have it another few times
This is fragment you're dealing with, not an activity. Fragment doesn't extend Context or View classes and does not have this findViewById method.
What you should do is to move your code from onCreateView to onViewCreated which gets a reference to the root view as a parameter. Then use the same method but with that view, as below:
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
Spinner spinner = view.findViewById(R.id.spinner1);
...
}
Another thing you may do is replacing:
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.frag2_layout, container, false);
Spinner spinner = findViewById (R.id.spinner1);
// I'm not sure how you didn't see all errors when writing code after a return statement.
...
}
With:
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag2_layout, container, false);
Spinner spinner = view.findViewById(R.id.spinner1);
...
return view; // after finishing all logic.
}
UPDATE:
Didn't see that there is also a problem with the ArrayAdapter.
As mentioned in Csongi77 answer, you need to to instantiate the adapter properly. Again, the fragment cannot use as context, you must use the containing activity for that:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.insulin, android.R.layout.simple_spinner_item);
ArrayAdapter.createFromResource creates and returns a new adapter and currently you just disposing it immediately by not saving its reference.
I think first you should instantiate adapter like this: ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.insulin, android.R.layout.simple_spinner_item); please let me know if it works. Regards, Csongi

Cannot retrieve View in onResume() method inside Fragment

I have a method in my fragment that I would like to call from onResume(). Unfortunately, my method requires that I pass it a View, which is defined in onCreateView():
onResume():
#Override
public void onResume() {
super.onResume();
retrieveData(0, false, rootView);
}
onCreateView():
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Set view
View rootView = inflater.inflate(R.layout.tab_fragment_1, container, false);
...
}
Is there a way to call my retrieveData method as is in onResume(), or should I just create a new method without the rootView parameter (not ideal)?
Can't move it to global scope because the inflater method parameter is part of the fragment interface.
You can only get the view from your fragment, not your activity. To do this, in your fragment onCreateView
save the root view as an Instance Variable like
private View root;
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_details_buddy, container, false);
... }
And call it in your onResume
Edit : to get data from Activity, pass it in a Bundle. Doc

How to call a tabbed activity fragment from main activity?

I have selected a tabbed activity template with "Action bar tabs" in android studio.
Then i had created three fragments (One.java, Two.java, Three.java) one for each of the tabs.
I have the code related to the cursors in the second fragment.The thing is when i run the application the code in the second fragment is not being executed.
My question is will the fragment (Two.java) will be called implicitly or it is needed to call by creating an instance (if yes, how?).
code snippet for MainActivity.java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(getArguments().getInt(ARG_SECTION_NUMBER)==1){
View rootView = inflater.inflate(R.layout.fragment_one, container, false);
return rootView;
}
else if (getArguments().getInt(ARG_SECTION_NUMBER) == 2) {
View rootView = inflater.inflate(R.layout.fragment_two, container, false);
return rootView;
} else {
View rootView = inflater.inflate(R.layout.fragment_three, container, false);
return rootView;
}
}
code snippet for Two.java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
logReader1 = new LogReader(getContext());
db = logReader1.getReadableDatabase();
Cursor logCursor = db.rawQuery("SELECT rowid _id,name,Time_Used,app_Image FROM logs ORDER BY Time_Used DESC", null);
LogCursorAdapter logAdapter = new LogCursorAdapter((MainActivity) getContext(), logCursor);
ListView log_Items = (ListView) container.findViewById(R.id.list_item);
log_Items.setAdapter((logAdapter));
return log_Items;
}
From what I can tell you aren't actually creating a new fragment. You're inflating the layout that you're intending to use as the layout for the fragment two.
Something like https://developer.android.com/training/implementing-navigation/lateral.html should get you pointed in the right direction. Notice how they create and return a fragment in the DemoCollectionPagerAdapter.

Fragment content does not show up until I switch it and select it again

I've got a problem with my Android app: my fragment content doesn't show up until I switch it and I select it again. What should load just at the fragment start is a list of Cards.
Here you got the onCreate method of the Fragment that loads everything:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bundle = this.getArguments();
debts = bundle.getParcelableArrayList("list");
adapter = new CardAdapter(getActivity(), android.R.color.holo_blue_dark);
for (int i=0; i<debts.size();++i) {
Debt d = debts.get(i);
Card card = new Card(d.getSecondUser(),d.getSubject());
adapter.add(card);
}
}
And here the onActivityCreated method where i set the adapter for the list:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
CardListView list = (CardListView) getView().findViewById(R.id.newCard);
list.setCardTheme(CardTheme.Light);
list.setAdapter(adapter);
}
It works if I use an ArrayList of Cards already loaded, but not this way.
I tried nearly everything... thank you very much.
UPDATE: Even I tried with the solutions you gave me, it keeps happening, my code now:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bundle = this.getArguments();
debts = bundle.getParcelableArrayList("list");
adapter = new CardAdapter(getActivity(), android.R.color.holo_blue_dark);
for (int i=0; i<debts.size();++i) {
Debt d = debts.get(i);
Card card = new Card(d.getSecondUser(),d.getSubject());
adapter.add(card);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_debts, container,
false);
CardListView list = (CardListView) rootView.findViewById(R.id.newCard);
list.setCardTheme(CardTheme.Light);
list.setAdapter(adapter);
return rootView;
}
You should initialize your cardListView inside the onCreateView() method. onCreateView() gets called before the onActivityCreated() method. onCreateView() should be the point when the view of your fragment is initialized.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//use your layout file instead of R.layout.fragment_main
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
CardListView list = rootView.findViewById(R.id.newCard);
list.setCardTheme(CardTheme.Light);
list.setAdapter(adapter);
}
Please take a look at the lifecycle of Fragments.

Categories