Display MySQL Data in Recycleview using fragments - android

I want to display my data inside a fragment but it seems like some methods are not applicable in fragments only in Activity. The text with * are causing me errors. How i can execute it on a fragment?
String urlAddress = "http://capstonproject.xyz/project/tourist/retrieve/adventure";
RecyclerView recyclerView;
public Fadventure() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//RecyclerView recyclerView = (RecyclerView) inflater.inflate(R.layout.fragment_fadventure, container, false);
return inflater.inflate(R.layout.fragment_fadventure, container, false);
recyclerView = view.findViewById(R.id.adventure_list);
recyclerView.setLayoutManager(new LinearLayoutManager(***this***));
recyclerView.setItemAnimator(new DefaultItemAnimator());
new DBFetch(***Fadventure.this***,recyclerView).execute();
//return recyclerView;
}

instead of this try with getActivity().getApplication().
hope it helps.

Related

RecyclerView inside of Fragment only gets populated if adapter and RecyclerView are setup in onCreate

I have a custom DialogFragment that has it's own RecyclerView, and for some reason the data does not get populated unless the adapter/recyclerView logic is performed in onCreate. Nothing will happen if it is called in onCreateView or onViewCreated, which is where I want all of this logic to happen. Here is the version that works in onCreate:
#Override public void onCreate(#Nullable Bundle savedInstanceState) {
root = (ViewGroup) getLayoutInflater().inflate(R.layout.dialog_content, null);
ButterKnife.bind(this, root);
MyAdapter adapter = new MenuOptionsAdapter(getContext(), this);
adapter.setData(getData());
rv.setAdapter(adapter);
rv.setLayoutManager(new LinearLayoutManager(getContext()));
}
And here is my try in onCreateView:
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
root = (ViewGroup) inflater.inflate(R.layout.dialog_content, container, false);
ButterKnife.bind(this, root);
// 'this' in the initialization of MyAdapter represents a custom interface, don't worry about it
MyAdapter adapter = new MenuOptionsAdapter(getContext(), this);
adapter.setData(getData());
rv.setAdapter(adapter);
rv.setLayoutManager(new LinearLayoutManager(getContext()));
}
I tried to initialize root like this as well in onCreateView: root = (ViewGroup) getLayoutInflater().inflate(R.layout.dialog_content, null);
But no dice.
Does anyone know what could be a possible reason for this? I also tried adding the same exact code in onViewCreated, but again, no luck.
PS - I also tried adding adapter.notifyDataSetChanged() after setting the data and also tried it after rv.setData.
Do the following:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
root = (ViewGroup) LayoutInflater.from(getActivity()).inflate(R.layout.dialog_content, null, false);
ButterKnife.bind(this, root);
// 'this' in the initialization of MyAdapter represents a custom interface, don't worry about it
MyAdapter adapter = new MenuOptionsAdapter(getContext(), this);
adapter.setData(getData());
rv.setAdapter(adapter);
rv.setLayoutManager(new LinearLayoutManager(getContext()));
return new AlertDialog.Builder(getActivity())
.setView(root)
.show();
}
If you wand to add view from layout, you should pass 'true' to last parametr in method inflater.inflate(R.layout.dialog_content, container, true);

avoid fragment list data duplicating on recyclerView

I had some fragments switch on one activity.
I found a issue is that when i turn back the fragment , it will show duplicate data.
I try to clear the arrayList data to solve it.
But i want to know more smart way.
Is any possible to avoid this issue for duplicate data ?
My fragment code:
public class Vaccine extends Fragment {
private List<VaccineItem> vaccineList = new ArrayList<>();
private VaccineAdapter vaccineAdapter;
private RecyclerView recyclerVaccine;
public Vaccine() {
}
public static Vaccine newInstance() {
return new Vaccine();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.vaccine_fragment_, container, false);
recyclerVaccine = (RecyclerView) view.findViewById(R.id.recyclerVaccine);
recyclerVaccine.setLayoutManager(new LinearLayoutManager(getActivity()));
vaccineList.clear();// i use it to slove the problem.--------------
testData();
vaccineAdapter = new VaccineAdapter(getActivity(), vaccineList);
recyclerVaccine.setAdapter(vaccineAdapter);
vaccineAdapter.notifyDataSetChanged();
return view;
}
private void testData(){
VaccineItem vaccineItem=new VaccineItem("Data1");
vaccineList.add(vaccineItem);
vaccineItem=new VaccineItem("Data2");
vaccineList.add(vaccineItem);
}
}
Try with this
View view; // declare this globally
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(view == null){ // initialize if view is null
view = inflater.inflate(R.layout.vaccine_fragment_, container, false);
recyclerVaccine = (RecyclerView) view.findViewById(R.id.recyclerVaccine);
recyclerVaccine.setLayoutManager(new LinearLayoutManager(getActivity()));
vaccineList.clear();// i use it to slove the problem.--------------
testData();
vaccineAdapter = new VaccineAdapter(getActivity(), vaccineList);
recyclerVaccine.setAdapter(vaccineAdapter);
vaccineAdapter.notifyDataSetChanged();
}
return view;
}
Call testData() in your constructor, not in onCreateView.
The reason you are getting duplicate data is because you are calling testData() when fragment view is created. This adds same items everytime your fragment is recreated. There is nothing wrong with clearing the list though.

"No adapter attached; skipping layout" on a fragment

public class WorkFragment extends Fragment {
List<CardViewItem> items = new ArrayList<>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("FRAGMENT", "Work Fragment started");
TypedArray icons = getResources().obtainTypedArray(R.array.project_icons);
TypedArray names = getResources().obtainTypedArray(R.array.project_names);
TypedArray descs = getResources().obtainTypedArray(R.array.project_descs);
for (int i=0;i<icons.length();i++){
items.add(new CardViewItem(icons.getDrawable(i),
names.getString(i),
descs.getString(i)));
}
icons.recycle();
names.recycle();
descs.recycle();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("FRAGMENT", "Work Fragment onCreateView");
View rootView = inflater.inflate(R.layout.fragment_work, container, false);
RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
LinearLayoutManager llm = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(llm);
recyclerView.setAdapter(new ItemAdapter(items));
return inflater.inflate(R.layout.fragment_work, container, false);
}
}
Gives me
E/RecyclerView: No adapter attached; skipping layout
I have tried out all the solutions I had found (setting an empty adapter, moving the code elswhere, using a seperate thread) but to no avail. This should work on a normal activity so I guess maybe I'm doing something wrong.
the problem is mostly this line
return inflater.inflate(R.layout.fragment_work, container, false);
you should have
return rootView
With the first return you are inflating a new totally different view hierarchy, starting from fragment_work.xml, from the one which has an the RecyclerView correctly set up.

NullPointerException when adding RecyclerView to Fragment

In my fragment's onCreateView method, I am inflating a layout that contains a RecyclerView. However, when I try to reference the view, I get a NullPointerException at the following line:
mRecyclerView.setLayoutManager(mStaggeredLayoutManager);
What am I doing wrong?
Here is my code:
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstance){
final StaggeredGridLayoutManager mStaggeredLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
mRecyclerView = (RecyclerView)getActivity().findViewById(R.id.list);
mRecyclerView.setLayoutManager(mStaggeredLayoutManager);
mRecyclerView.setHasFixedSize(true); //Data size is fixed - improves performance
mAdapter = new TravelListAdapter(getActivity());
mRecyclerView.setAdapter(mAdapter);
return inflater.inflate(R.layout.start_fragment,container,false);
}
You are doing getActivity.findViewById(), which wouldn't work. You just created the view, but it is not attached to the activity yet. For this to work, you have to find the view in the fragment's view, not the activity's view.
First, inflate the fragment's view, and then do inflatedView.findViewById(R.id.list).
Example:
public View onCreateView(...){
View inflatedView = inflater.inflate(R.layout.start_fragment, container, false);
mRecyclerView = (RecyclerView) inflatedView.findViewById(R.id.list);
return inflatedView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
final StaggeredGridLayoutManager mStaggeredLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
mRecyclerView = (RecyclerView) view.findViewById(R.id.list);
mRecyclerView.setLayoutManager(mStaggeredLayoutManager);
mRecyclerView.setHasFixedSize(true);
mAdapter = new TravelListAdapter(getActivity());
mRecyclerView.setAdapter(mAdapter);
super.onViewCreated(view, savedInstanceState);
}
this is what I would do

Kinvey data being loaded in RecyclerView

I m have a problem with my RecyclerView. I m trying to populate it with a list of friends names, however when i launch my fragment friends are not loaded. On the other side friends are loaded if i m entering debug mode.
My code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mKinveyClient = KinveyUtils.getClient(getContext());
mFriends = getUserFriends();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_friend_list, container, false);
// Set the adapter
if (view instanceof RecyclerView) {
Context context = view.getContext();
mRecyclerView = (RecyclerView) view;
mRecyclerView.setLayoutManager(new LinearLayoutManager(context));
mRecyclerView.setAdapter(new FriendRecyclerViewAdapter(mFriends));
}
return view;
}
private ArrayList<ArrayMap> getUserFriends() {
return (ArrayList<ArrayMap>) mKinveyClient.user().get(KinveyConstants.FRIENDS);
}
I feel like the friends are loading too slow, but all the needed data should be already in app by this time. Any idea how to fix it?

Categories

Resources