No Adapter Attached, Not Sure Where This Problem Occurs
Solution 1:
You're receiving the 'No adapter attached' message because you're setting the adapter from an asynchronous callback. By the time the callback returns, Android has already completed its layout pass, seen that the RecyclerView hasn't been set up yet, and has already decided to skip it.
Instead, you should set your adapter directly within onCreateView
and update it from your callback.
Add an update method to your Adapter:
// Update your Adapter's member variable to instantiate a new ArrayList.// (This will let the Layout Manager know that there are 0 items in the adapter// initially. This way the layout can still be drawn without error before we add the// items from our response callback.)
ArrayList<VaccinationTimeline> recentRecords = new ArrayList<>();
// Update your constructor to receive no parameters// since the items will be updated later.publicVaccinationAdapter() {}
// ...// Create a method to update your Adapter's itemspublicvoidupdateRecords(ArrayList<VaccinationTimeline> recentRecords) {
this.recentRecords = recentRecords;
notifyDataSetChanged(); // Notify the Adapter that the item data has changed
}
Set the Adapter in your onCreateView
:
// Create a member variable for your Adapter
VaccinationAdapter vaccinationAdapter;
@Overridepublic View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Viewview= inflater.inflate(R.layout.fragment_vaccination, container, false);
// call view
rvRecentRecord = view.findViewById(R.id.vaccinationRecord);
progressBar = view.findViewById(R.id.progress_circular_vaccination);
rvRecentRecord.setLayoutManager(newLinearLayoutManager(getActivity()));
// Set your Adapter
vaccinationAdapter = newVaccinationAdapter();
rvRecentRecord.setAdapter(vaccinationAdapter);
// Volley Fetch
getDataFromServer();
return view;
}
Update the Adapter from your callback:
@OverridepublicvoidonResponse(String response) {
progressBar.setVisibility(View.GONE);
if (response != null) {
Log.e(TAG, "onResponse:" + response);
try {
JSONObject jsonObject = newJSONObject(response);
JSONObject subObject = jsonObject.getJSONObject("timeline");
JSONArray keys = subObject.names();
// Collect the new values from the callbackArrayList<VaccinationTimeline> newRecords = newArrayList<>():
for (int i = 0; i < keys.length(); i++) {
String key = keys.getString(i);
int value = subObject.getInt(key);
newRecords.add(newVaccinationTimeline(key, value));
}
// Update the adapter with the new values
vaccinationAdapter.updateRecords(newRecords);
}catch (JSONException e) {
e.printStackTrace();
}
}
}
Solution 2:
**No package ID 07 found for ID 0x073d0cd9 This problem is fixed.
I try set integer using setText(),
Original:
holder.tvCount.setText(recentRecord.getmCount());
Fixed:
holder.tvCount.setText(String.valueof(recentRecord.getmCount());
Still having no adapter attached problem. Anyone familiar with this? I double check my layout file, and make sure I attached the right xml. Maybe I am missing something?
Post a Comment for "No Adapter Attached, Not Sure Where This Problem Occurs"