i want create a fragment and have inside a recycler view for show products ...
but when i render it is show this error : RecyclerView: No adapter attached; skipping layout
below i copy my codes;
this code is for adapter class :
private ArrayList<Products> ProductsList;
private Context context;
public Adapter(ArrayList<Products> productsList, Context context) {
ProductsList = productsList;
this.context = context;
}
#Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.row_layout, parent, false);
return new MyHolder(v);
}
#Override
public void onBindViewHolder(MyHolder holder, final int position) {
Products products = ProductsList.get(position);
holder.txtName.setText(products.getName());
holder.txtPrice.setText("$ " + products.getPrice());
Picasso.get().load(Config.ipValue + "/images/" + products.getPhoto()).into(holder.imgV);
holder.imgV.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.startAnimation(AnimationUtils.loadAnimation(context, android
.R.anim.slide_in_left));
}
});
}
#Override
public int getItemCount() {
return ProductsList.size();
}
class MyHolder extends RecyclerView.ViewHolder {
TextView txtName;
TextView txtPrice;
ImageView imgV;
public MyHolder(View itemView) {
super(itemView);
txtName = itemView.findViewById(R.id.rowTxtProductName);
txtPrice = itemView.findViewById(R.id.rowTxtPrice);
imgV = itemView.findViewById(R.id.rowImgProduct);
}
}
and this one for web api class :
public class WebApiHandler {
Context context;
String apiLink = "";
ArrayList<Products> products = new ArrayList<>();
public WebApiHandler(Context context) {
this.context = context;
}
void apiConnect(String type) {
switch (type) {
case "getproducts": {
apiLink = Config.getProductsWebApi;
break;
}
}
ProgressDialog dialog = ProgressDialog.show(context, "Connecting...",
"please wait", false, false);
StringRequest request = new StringRequest(Request.Method.POST,
apiLink, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
dialog.dismiss();
showJson(response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
dialog.dismiss();
}
});
RequestQueue queue = Volley.newRequestQueue(context);
queue.add(request);
}
private void showJson(String response) {
products.clear();
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("response");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
String id = object.getString("id");
String name = object.getString("name");
String description = object.getString("description");
String price = object.getString("price");
String photo = object.getString("photo");
Products p = new Products(id, name, description, price, photo);
products.add(p);
}
} catch (JSONException e) {
e.printStackTrace();
}
VerticalFragment.productsArrayList = products;
IData iData = (IData) context;
iData.sendData();
}}
and my fragment code :
public class VerticalFragment extends Fragment implements IData {
RecyclerView rcVertical;
WebApiHandler webApiHandler;
static ArrayList<Products> productsArrayList = new ArrayList<>();
public VerticalFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_vertical, container, false);
rcVertical = view.findViewById(R.id.rcVertical);
webApiHandler = new WebApiHandler(getContext());
webApiHandler.apiConnect("getproducts");
rcVertical.addOnItemTouchListener(new RecyclerTouchListener(getContext(), rcVertical,
new RecyclerTouchListener.ClickListener() {
#Override
public void onClick(View view, int position) {
ProductActivity.products = productsArrayList.get(position);
startActivity(new Intent(getContext(), ProductActivity.class));
}
#Override
public void onLongClick(View view, int position) {
}
}));
return view;
}
#Override
public void sendData() {
Adapter adapter = new Adapter(productsArrayList, getContext());
rcVertical.setLayoutManager(new LinearLayoutManager((getContext())));
rcVertical.setItemAnimator(new DefaultItemAnimator());
rcVertical.setAdapter(adapter);
}}
i should say i create a interface and have one method i say it sendData
This is a common error when there is no adapter attached to recyclerview upon showing recyclerview. It will not cause any harm to your app, but you could avoid it by setting empty adapter without data, and later when you get your data, provide it to your adapter and notify it
Edit - Example added
Create setter for your list in adapter. After that, in your fragment make adapter global and in onCreateView make instance of your adapter and attach it to recyclerview
adapter = new Adapter(new ArrayList<>(), getContext());
rcVertical.setAdapter(adapter);
...Initialize recyclerview...
And then in your sendData interface put your loaded values in it
adapter.setData(productsArrayList);
adapter.notifyDataSetChanged();
Related
I'm getting the error:
RecyclerView﹕ No adapter attached; skipping layout
I checked some other answers people have given for this issue, but had no luck.
Adapter:
public class MyAdapter extends RecyclerView.Adapter<com.example.myapplication.MyAdapter.MyViewHolder> {
private Context context;
private List<HeadlineModel> headlineModelList;
public MyAdapter(Context context, List<HeadlineModel> headlineModelList) {
this.context = context;
this.headlineModelList = headlineModelList;
}
#NonNull
#Override
public com.example.myapplication.MyAdapter.MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(context).inflate(R.layout.news_item_layout,parent,false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final com.example.myapplication.MyAdapter.MyViewHolder holder, int position) {
final HeadlineModel headlineModel=headlineModelList.get(position);
holder.newTitle.setText(headlineModel.getTitle());
holder.newsDescription.setText(headlineModel.getDescription());
holder.newsDescription.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Uri uri= Uri.parse(headlineModel.getUrl());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(intent);
//Log.v("SSSSSS",headlineModel.getUrl());
}
});
holder.newsName.setText(headlineModel.getName());
holder.newsTime.setText(headlineModel.getPublishedAt());
Glide.with(context).load(headlineModel.getUrlToImage())
.thumbnail(0.5f)
.into(holder.newsImage);
}
#Override
public int getItemCount() {
return headlineModelList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView newTitle;
TextView newsDescription;
TextView newsName;
TextView newsTime;
ImageView newsImage;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
newTitle=itemView.findViewById(R.id.tv_news_title);
newsDescription=itemView.findViewById(R.id.tv_news_desc);
newsName=itemView.findViewById(R.id.tv_name);
newsTime=itemView.findViewById(R.id.tv_news_date);
newsImage=itemView.findViewById(R.id.im_news_image);
}
}
}
The recycler view in onCreateView in my fragment is initialized like this:
RecyclerView recyclerView;
com.example.myapplication.MyAdapter myAdapter;
private ProgressDialog progressDialog;
List<com.example.myapplication.HeadlineModel> headlineModelList;
private static final String URL="http://newsapi.org/v2/top-headlines?country=in&apiKey=cbd46bd6a4f54fe69d0cb261dbe1a878";
public HeadlinesFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview=inflater.inflate(R.layout.fragment_headlines, container, false);
// Inflate the layout for this fragment
recyclerView=rootview.findViewById(R.id.headRecyclerView);
headlineModelList=new ArrayList<>();
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL,false));
progressDialog=new ProgressDialog(getContext(),R.style.ProgressColor);
progressDialog.setMessage("loading...");
progressDialog.show();
loadData();
return rootview;
}
loadData() function that is called
private void loadData() {
progressDialog.setMessage("Loading data...");
//progressDialog.show();
JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.GET, URL, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
progressDialog.dismiss();
try {
JSONArray jsonArray=response.getJSONArray("articles");
for (int i=0;i<jsonArray.length();i++){
JSONObject jsonObject=jsonArray.getJSONObject(i);
String title=jsonObject.getString("title");
String desc=jsonObject.getString("description");
String url=jsonObject.getString("url");
String urlToImage=jsonObject.getString("urlToImage");
String publishedAt=jsonObject.getString("publishedAt");
JSONObject source=jsonObject.getJSONObject("source");
String name=source.getString("name");
// for formatting time and date //
String year=publishedAt.substring(0,4);
String month=publishedAt.substring(5,7);
String date=publishedAt.substring(8,10);
String hour=publishedAt.substring(11,13);
//String hour="11";
String min=publishedAt.substring(14,16);
//Log.v("XXXXXX",min);
String updatedDate=date.concat("-").concat(month).concat("-").concat(year).concat(" ");
String print="";
int convertHour= Integer.parseInt(hour);
int convertMin= Integer.parseInt(min);
if (convertHour==12) {
convertHour=12;
print="PM";
}
else if (convertHour>11&&convertMin>0){
convertHour=convertHour-12;
print="PM";
}else {
print="AM";
}
String newHour= String.valueOf(convertHour);
String updatedTime=updatedDate.concat(newHour).concat(":").concat(min).concat(" ").concat(print);
myAdapter=new com.example.myapplication.MyAdapter(getContext(),headlineModelList);
com.example.myapplication.HeadlineModel headlineModel=new com.example.myapplication.HeadlineModel(name,title,desc,url,urlToImage,updatedTime);
headlineModelList.add(headlineModel);
recyclerView.setAdapter(myAdapter);
myAdapter.notifyDataSetChanged();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
})
{
#Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try {
Cache.Entry cacheEntry = HttpHeaderParser.parseCacheHeaders(response);
if (cacheEntry == null) {
cacheEntry = new Cache.Entry();
}
final long cacheHitButRefreshed = 3 * 60 * 1000; // in 3 minutes cache will be hit, but also refreshed on background
final long cacheExpired = 24 * 60 * 60 * 1000; // in 24 hours this cache entry expires completely
long now = System.currentTimeMillis();
final long softExpire = now + cacheHitButRefreshed;
final long ttl = now + cacheExpired;
cacheEntry.data = response.data;
cacheEntry.softTtl = softExpire;
cacheEntry.ttl = ttl;
String headerValue;
headerValue = response.headers.get("Date");
if (headerValue != null) {
cacheEntry.serverDate = HttpHeaderParser.parseDateAsEpoch(headerValue);
}
headerValue = response.headers.get("Last-Modified");
if (headerValue != null) {
cacheEntry.lastModified = HttpHeaderParser.parseDateAsEpoch(headerValue);
}
cacheEntry.responseHeaders = response.headers;
final String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONObject(jsonString), cacheEntry);
} catch (UnsupportedEncodingException | JSONException e) {
return Response.error(new ParseError(e));
}
}
#Override
protected void deliverResponse(JSONObject response) {
super.deliverResponse(response);
}
#Override
public void deliverError(VolleyError error) {
super.deliverError(error);
}
#Override
protected VolleyError parseNetworkError(VolleyError volleyError) {
return super.parseNetworkError(volleyError);
}
};
RequestQueue queue= Volley.newRequestQueue(getContext());
queue.add(jsonObjectRequest);
}
Does anyone know what I'm doing wrong?
Set the Adapter before the API call success because your API response is async and till will take time depending upon response size, network speed etc, till that time recycler view needs to be created and it is having no adapter.
You can set adaptor before calling loadData() and later when you get the list of response set the data in the adapter of your recylerview and call notify on the adapter.
myAdapter=new com.example.myapplication.MyAdapter(getContext(),headlineModelList);
recyclerView.setAdapter(myAdapter);
use it on onCreateView method and myAdapter.notifyDataSetChanged(); use it when API success
When you click on the CardView, a DialogFragment should appear, but the application crashes, writes the following in the logs
Process: com.example.testfuntura, PID: 15385
java.lang.ClassCastException: android.view.ContextThemeWrapper cannot be cast to androidx.appcompat.app.AppCompatActivity
at com.example.testfuntura.Attraction.AdapterAttractionsPackageOffer.lambda$onBindViewHolder$0(AdapterAttractionsPackageOffer.java:56)
at com.example.testfuntura.Attraction.-$$Lambda$AdapterAttractionsPackageOffer$4cZEQxk2beWA40yZmNkr6ZJp7o8.onClick(lambda)
at android.view.View.performClick(View.java:5692)
at android.view.View$PerformClick.run(View.java:22596)
My Adapter
public class AdapterAttractionsPackageOffer extends RecyclerView.Adapter<AdapterAttractionsPackageOffer.AttractionsViewHolderPackageOffer> {
public ArrayList<ItemPOA> mFavList;
public AdapterAttractionsPackageOffer(ArrayList<ItemPOA> favList) {
mFavList = favList;
}
#NonNull
#Override
public AttractionsViewHolderPackageOffer onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_attraction_package_offer, parent, false);
return new AttractionsViewHolderPackageOffer(v);
}
public static class AttractionsViewHolderPackageOffer extends RecyclerView.ViewHolder {
public ImageView card_image_1;
public TextView Package_offer_title;
public CardView Card;
public AttractionsViewHolderPackageOffer(View itemView) {
super(itemView);
Card = itemView.findViewById(R.id.att_po);
card_image_1 = itemView.findViewById(R.id.img_po);
Package_offer_title = itemView.findViewById(R.id.tex);
}
}
#SuppressLint("LongLogTag")
#Override
public void onBindViewHolder(AttractionsViewHolderPackageOffer holder, int position) {
ItemPOA currentItem = mFavList.get(position);
holder.Package_offer_title.setText(currentItem.get_Package_offer_title());
holder.Card.setOnClickListener(v -> {
DialogPOA dialog = new DialogPOA();
dialog.show(((AppCompatActivity) v.getContext()).getSupportFragmentManager(), "item");
Bundle bundle1 = new Bundle();
bundle1.putString("title_po", currentItem.get_Package_offer_title());
bundle1.putString("description_po", currentItem.get_Package_offer_description());
bundle1.putString("cost_po", currentItem.get_Package_offer_cost());
dialog.setArguments(bundle1);
});
holder.getAdapterPosition();
}
#Override
public int getItemCount() {
return mFavList.size();
}
}
I have a similar Adapter, but when called, everything works, that is, the problem is not in the DialogFragment, since everything works when called from another fragment.
Fragment that the CardView is in
public class FragmentAtt extends Fragment {
ArrayList<ItemPOA> mFavList;
String URL = "http://qqqqqqqqq.mcdir.ru/dbpo.php";
RecyclerView mRec;
RecyclerView.LayoutManager recyclerViewlayoutManager;
RecyclerView.Adapter recyclerViewadapter;
//ImageView GET_PO_IMG;
String GET_PO_TITLE = "title_po";
String GET_PO_DESCRIPTION = "description_po";
String GET_PO_COST = "cost_po";
RequestQueue requestQueue;
JsonArrayRequest jsonArrayRequest;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_test_attraction, container, false);
TabLayout tabLayout = view.findViewById(R.id.tab);
ViewPager viewPager = view.findViewById(R.id.pager);
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
return view;
}
#Override
public void onViewCreated(#NonNull final View view, #Nullable Bundle savedInstanceState) {
mRec = requireView().findViewById(R.id.rec_po);
mFavList = new ArrayList<>();
recyclerViewadapter = new AdapterAttractionsPackageOffer(mFavList);
recyclerViewlayoutManager = new LinearLayoutManager(getContext());
mRec.setHasFixedSize(true);
mRec.setLayoutManager(recyclerViewlayoutManager);
mRec.setAdapter(recyclerViewadapter);
JSON_DATA_WEB_CALL2_RINK();
//recyclerViewlayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
//SnapHelper snapHelper = new GravitySnapHelper(Gravity.START);
//snapHelper.attachToRecyclerView(mRec);
}
private void setupViewPager(ViewPager viewPager) {
PagerAdapter adapter = new PagerAdapter(getChildFragmentManager());
adapter.addFragment(new FragmentAttractionRecyclerView(), "Аттракционы");
adapter.addFragment(new FragmentAttractionRecyclerView(), "Видеоигры");
viewPager.setAdapter(adapter);
}
public void JSON_DATA_WEB_CALL2_RINK() {
jsonArrayRequest = new JsonArrayRequest(URL,
this::JSON_PARSE_DATA_AFTER_WEBCALL_RINK,
error ->
Log.e("Volley", error.toString())) {
#Override
protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
try {
Cache.Entry cacheEntry = HttpHeaderParser.parseCacheHeaders(response);
if (cacheEntry == null) {
cacheEntry = new Cache.Entry();
}
final long cacheExpired = 24 * 60 * 60 * 1000; // in 24 hours this cache entry expires completely
long now = System.currentTimeMillis();
final long ttl = now + cacheExpired;
cacheEntry.data = response.data;
cacheEntry.softTtl = now;
cacheEntry.ttl = ttl;
String headerValue;
headerValue = response.headers.get("Date");
if (headerValue != null) {
cacheEntry.serverDate = HttpHeaderParser.parseDateAsEpoch(headerValue);
}
headerValue = response.headers.get("Last-Modified");
if (headerValue != null) {
cacheEntry.lastModified = HttpHeaderParser.parseDateAsEpoch(headerValue);
}
cacheEntry.responseHeaders = response.headers;
final String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONArray(jsonString), cacheEntry);
} catch (UnsupportedEncodingException | JSONException e) {
return Response.error(new ParseError(e));
}
}
};
requestQueue = Volley.newRequestQueue(requireContext());
requestQueue.add(jsonArrayRequest);
}
public void JSON_PARSE_DATA_AFTER_WEBCALL_RINK(JSONArray array) {
mFavList.clear();
for (int i = 0; i < array.length(); i++) {
ItemPOA GetDataAdapter2 = new ItemPOA();
JSONObject json;
try {
json = array.getJSONObject(i);
GetDataAdapter2.set_Package_offer_title(json.getString(GET_PO_TITLE));
GetDataAdapter2.set_Package_offer_description(json.getString(GET_PO_DESCRIPTION));
GetDataAdapter2.set_Package_offer_cost(json.getString(GET_PO_COST));
} catch (JSONException e) {
e.printStackTrace();
}
mFavList.add(GetDataAdapter2);
}
recyclerViewadapter = new AdapterAttractionsPackageOffer(mFavList);
mRec.setAdapter(recyclerViewadapter);
}
}
An Adapter that has the same functionality, but why does it work, explain the difference?
public class AdapterSkatingRinkSchedule_2 extends RecyclerView.Adapter<AdapterSkatingRinkSchedule_2.SkatingRinkScheduleViewHolder> {
public ArrayList<ItemSkatingRinkSchedule_2> mSkatingRinkScheduleList;
public AdapterSkatingRinkSchedule_2(ArrayList<ItemSkatingRinkSchedule_2> SkatingRinkScheduleList) {
mSkatingRinkScheduleList = SkatingRinkScheduleList;
}
#NonNull
#Override
public SkatingRinkScheduleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_srs, parent, false);
return new SkatingRinkScheduleViewHolder(v);
}
public static class SkatingRinkScheduleViewHolder extends RecyclerView.ViewHolder {
public TextView number_srs, start_time_srs, end_time_srs, cost_1_srs, cost_2_srs;
public LinearLayout ll_main;
public SkatingRinkScheduleViewHolder(View itemView) {
super(itemView);
ll_main = itemView.findViewById(R.id.ll_main);
number_srs = itemView.findViewById(R.id.number_srs);
start_time_srs = itemView.findViewById(R.id.start_time_srs);
end_time_srs = itemView.findViewById(R.id.end_time_srs);
cost_1_srs = itemView.findViewById(R.id.cost_1_srs);
cost_2_srs = itemView.findViewById(R.id.cost_2_srs);
}
}
#SuppressLint("LongLogTag")
#Override
public void onBindViewHolder(SkatingRinkScheduleViewHolder holder, int position) {
ItemSkatingRinkSchedule_2 currentItem = mSkatingRinkScheduleList.get(position);
holder.number_srs.setText(currentItem.get_session_number());
holder.start_time_srs.setText(currentItem.get_session_start());
holder.end_time_srs.setText(currentItem.get_session_end());
holder.cost_1_srs.setText(currentItem.get_weekdays());
holder.cost_2_srs.setText(currentItem.get_weekends_and_holidays());
holder.ll_main.setOnClickListener(v -> {
DialogSkatingRinkSchedule dialogSRS = new DialogSkatingRinkSchedule();
dialogSRS.show(((AppCompatActivity) v.getContext()).getSupportFragmentManager(), "item");
Bundle bundle2 = new Bundle();
bundle2.putString("number", currentItem.get_session_number());
bundle2.putString("start", currentItem.get_session_start());
bundle2.putString("end", currentItem.get_session_end());
bundle2.putString("w", currentItem.get_weekdays());
bundle2.putString("wah", currentItem.get_weekends_and_holidays());
dialogSRS.setArguments(bundle2);
});
holder.getAdapterPosition();
}
#Override
public int getItemCount() {
return mSkatingRinkScheduleList.size();
}
}
java.lang.ClassCastException: android.view.ContextThemeWrapper cannot be cast to androidx.appcompat.app.AppCompatActivity
Here you are casting a context to an AppCompatActivity but actually Activity is not a Context
You have two options to solve this:
First option: Create a listener interface between the activity/fragment that holds the RecyclerView, implement it in that activity, and trigger its callback within the CardView onClickListener onClick() callback.
Second option: Change the constructor signature of the AdapterAttractionsPackageOffer adapter to accept a FragmentManager instance; save it as a class field like below, and reflect that on the instantiation of the adapter
public class AdapterAttractionsPackageOffer extends RecyclerView.Adapter<AdapterAttractionsPackageOffer.AttractionsViewHolderPackageOffer> {
public ArrayList<ItemPOA> mFavList;
private FragmentManager fragmentMgr;
public AdapterAttractionsPackageOffer(ArrayList<ItemPOA> favList, FragmentManager fManger) {
mFavList = favList;
fragmentMgr = fManger;
}
//......... suppressed code
#SuppressLint("LongLogTag")
#Override
public void onBindViewHolder(AttractionsViewHolderPackageOffer holder, int position) {
ItemPOA currentItem = mFavList.get(position);
holder.Package_offer_title.setText(currentItem.get_Package_offer_title());
holder.Card.setOnClickListener(v -> {
DialogPOA dialog = new DialogPOA();
dialog.show(fragmentMgr, "item");
Bundle bundle1 = new Bundle();
bundle1.putString("title_po", currentItem.get_Package_offer_title());
bundle1.putString("description_po", currentItem.get_Package_offer_description());
bundle1.putString("cost_po", currentItem.get_Package_offer_cost());
dialog.setArguments(bundle1);
});
holder.getAdapterPosition();
}
//......... suppressed code
}
I have an Fragment with recycleview where I populate it with json items from internet.
It load fine and Next step I want to is open new Activity when any row is clicked. It works in activity, thus I modified the same code for fragment but for fragment it throws exception in line
mExampleAdapter.setOnItemClickListener(getActivity());
with errror setOnItemClickListener of refrence adatper cannot be applied to Fragment activty and thus when I change line to
(ExampleAdapter.OnItemClickListener)
and when i build and run . Then app crashes with error that Mainactivity which holds framgnet cannot be cast in to .ExampleAdapter$OnItemClickListener
Here is my whole Fragment class
public class Mynotes extends Fragment implements ExampleAdapter.OnItemClickListener{
public static final String YTD_LINK = "link";
private RecyclerView mRecyclerView;
private ExampleAdapter mExampleAdapter;
private ArrayList<ExampleItem> mExampleList;
private RequestQueue mRequestQueue;
String url="https://api.myjson.com/bins/16mecx";
public Mynotes() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.activity_jsonfeed, container, false);
mRecyclerView = view.findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.addItemDecoration(new MyDividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL, 36));
mExampleList = new ArrayList<>();
mExampleAdapter = new ExampleAdapter(getActivity(), mExampleList);
mRecyclerView.setAdapter(mExampleAdapter);
mExampleAdapter.setOnItemClickListener((ExampleAdapter.OnItemClickListener) getActivity());
parseJSON();
return view;
}
private void parseJSON() {
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
myProgressBar.setVisibility(View.GONE);
try {
JSONArray jsonArray = response.getJSONArray("hits");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject hit = jsonArray.getJSONObject(i);
String videoTitle = hit.getString("title");
String link = hit.getString("link");
mExampleList.add(new ExampleItem(videoTitle, link));
mExampleAdapter.notifyDataSetChanged();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mRequestQueue.add(request);
}
#Override
public void onItemClick(int position) {
Intent intent = new Intent(getActivity(), NewActiviyt.class);
ExampleItem clickedItem = mExampleList.get(position);
intent.putExtra(YTD_LINK, clickedItem.getmLink());
startActivity(intent);
}
#Override
public void onRefresh() {
}
}
and my Adapter Class is
public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {
private Context mContext;
private ArrayList<ExampleItem> mExampleList;
private OnItemClickListener mListener;
public interface OnItemClickListener {
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
mListener = listener;
}
public ExampleAdapter(Context context, ArrayList<ExampleItem> exampleList) {
mContext = context;
mExampleList = exampleList;
}
#Override
public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.example_item, parent, false);
return new ExampleViewHolder(v);
}
#Override
public void onBindViewHolder(ExampleViewHolder holder, int position) {
ExampleItem currentItem = mExampleList.get(position);
String title = currentItem.getTitle();
// int likeCount = currentItem.getLikeCount();
// String imageUrl = currentItem.getImageUrl();
holder.mTextViewCreator.setText(title);
// holder.mTextViewLikes.setText("Likes: " + likeCount);
// Glide.with(mContext).load(imageUrl).apply(RequestOptions.circleCropTransform()).into(holder.mImageView);
}
#Override
public int getItemCount() {
return mExampleList.size();
}
public class ExampleViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView mTextViewCreator;
// public TextView mTextViewLikes;
// public ImageView mImageView;
public ExampleViewHolder(View itemView) {
super(itemView);
// mTextViewLikes = itemView.findViewById(R.id.text_view_likes);
// mImageView = itemView.findViewById(R.id.image_view);
mTextViewCreator = itemView.findViewById(R.id.text_title);
}
#Override
public void onClick(View view) {
if (mListener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
mListener.onItemClick(position);
}
}
}
}
}
Thanks in advance.
replace getActivity with getContext when you work in fragment,
You can read more here
What is different between getContext and getActivity from Fragment in support library?
Your activity should implement ExampleAdapter.OnItemClickListener
I want to implement one code where data will come on Scroll and if the data is add on list then we pull down(SwipeRefreshLayout) the new data will come on that list. In my below code the add on data is coming in response but not come in list? I have read many tutorials like this but data is coming on Swipe refresh but not shown on list. Thank you in advance
public class UpdatesFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener{
SwipeRefreshLayout mSwipeRefreshLayout;
UserSessionManager session;
private List<SuperHero> listSuperHeroes;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;
public ProgressBar progressBar;
public String user_id;
TextView textView;
FloatingActionButton fab;
public int a=1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_updates, container, false);
session = new UserSessionManager(getActivity());
// get user data from session
HashMap<String, String> user = session.getUserDetails();
user_id = user.get(UserSessionManager.KEY_USER_ID);
// SwipeRefreshLayout
mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
mSwipeRefreshLayout.setOnRefreshListener(this);
mSwipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary,
android.R.color.holo_green_dark,
android.R.color.holo_orange_dark,
android.R.color.holo_blue_dark);
/**
* Showing Swipe Refresh animation on activity create
* As animation won't start on onCreate, post runnable is used
*/
mSwipeRefreshLayout.post(new Runnable() {
#Override
public void run() {
mSwipeRefreshLayout.setRefreshing(true);
// Fetching data from server
getDataFromServer(1);
}
});
return view;
}
public void onViewCreated(View v, Bundle savedInstanceState) {
super.onViewCreated(v, savedInstanceState);
fab = (FloatingActionButton)v.findViewById(R.id.fab);
recyclerView = (RecyclerView) v.findViewById(R.id.updatesRecycler);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
listSuperHeroes = new ArrayList<>();
adapter = new CardAdapter(listSuperHeroes, getActivity());
recyclerView.setAdapter(adapter);
textView = (TextView)v.findViewById(R.id.text);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getContext(), AddWebPostActivity.class);
startActivity(i);
}
});
getDataFromServer(a);
recyclerView.setOnScrollListener(new EndlessRecyclerOnScrollListener((LinearLayoutManager) layoutManager) {
#Override
public void onLoadMore(int current_page) {
getDataFromServer(current_page);
}
});
}
public void getDataFromServer(int requestCount) {
Log.e("count",String.valueOf(requestCount));
final String DATA_URL = "https://XYZ.php?username="+user_id + "&page="+requestCount;
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(DATA_URL,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
mSwipeRefreshLayout.setRefreshing(false);
Log.e("URL___________",DATA_URL);
Log.e("response___________", String.valueOf(response));
parseData(response);
adapter.notifyDataSetChanged();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(), "No More Items Available", Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.getCache().clear();
requestQueue.add(jsonArrayRequest);
}
private void parseData(JSONArray array) {
for (int i = 0; i < array.length(); i++) {
SuperHero superHero = new SuperHero();
JSONObject json = null;
try {
json = array.getJSONObject(i);
superHero.setPost_title(json.getString(Config.TAG_POST_TITLE));
superHero.setPost_content(json.getString(Config.TAG_POST_CONTENT));
superHero.setPost_parent(json.getString(Config.TAG_POST_PARENT));
superHero.setPost_date(json.getString(Config.TAG_POST_DATE));
superHero.setPost_date_gmt(json.getString(Config.TAG_POST_DATE_GMT));
superHero.setScheduled_date(json.getString(Config.TAG_SCHEDULED_DATE));
superHero.setTo_ping(json.getString(Config.TAG_IP_ADDRESS));
superHero.setVisit_post(json.getString(Config.TAG_VISIT_POST));
superHero.setUr_id(json.getString(Config.TAG_UR_ID));
} catch (JSONException e) {
e.printStackTrace();
}
listSuperHeroes.add(superHero);
}
}
/**
* Called when a swipe gesture triggers a refresh.
*/
#Override
public void onRefresh() {
getDataFromServer(1);
}
}
Adapter
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {
UserSessionManager session;
public String user_id;
public String ur_id;
private Context context;
//String visit;
//List to store all superheroes
List<SuperHero> superHeroes;
//Constructor of this class
public CardAdapter(List<SuperHero> superHeroes, Context context){
super();
//Getting all superheroes
this.superHeroes = superHeroes;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.web_post, parent, false);
// Session class instance
session = new UserSessionManager(context);
session.checkLogin();
// get user data from session
HashMap<String, String> user = session.getUserDetails();
user_id = user.get(UserSessionManager.KEY_USER_ID);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
//Getting the particular item from the list
final SuperHero superHero = superHeroes.get(position);
holder.tvPostTitle.setText(superHero.getPost_title());
holder.tvPostContent.setText(superHero.getPost_content());
holder.tvPostDateTime1.setText(superHero.getPost_date() +" / " +superHero.getPost_date_gmt());
final String visit = superHero.getVisit_post();
holder.tvVisitPost.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(context,VisitWebView.class);
i.putExtra("visitURL",visit);
context.startActivity(i);
}
});
}
#Override
public int getItemCount() {
return superHeroes.size();
}
#Override
public int getItemViewType(int position) {
return position;
}
class ViewHolder extends RecyclerView.ViewHolder{
//Views
public TextView tvPostTitle,tvPostContent,tvPostDateTime1,tvVisitPost;
public ImageButton removeButton;
//Initializing Views
public ViewHolder(final View itemView) {
super(itemView);
tvPostTitle = (TextView) itemView.findViewById(R.id.tvPostTitle);
tvPostContent = (TextView) itemView.findViewById(R.id.tvPostContent);
tvPostDateTime1 = (TextView) itemView.findViewById(R.id.tvPostDateTime1);
tvVisitPost = (TextView) itemView.findViewById(R.id.tvVisitPost);
removeButton = (ImageButton) itemView.findViewById(R.id.removeButton);
}
}
}
use this method
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
mSwipeRefreshLayout.setRefreshing(true);
getDataFromServer(1);
}
instead of this
mSwipeRefreshLayout.post(new Runnable() {
#Override
public void run() {
mSwipeRefreshLayout.setRefreshing(true);
// Fetching data from server
getDataFromServer(1);
}
});
Please call adapter and pass fresh list data after this line
listSuperHeroes.add(superHero) in parseData(response) function ;
adapter = new CardAdapter(listSuperHeroes, getActivity());
recyclerView.setAdapter(adapter)
adapter.notifyDataSetChanged();
I am creating an app where a list of hotels will be shown, all the data is coming from MySQL using JSON and PHP, I created the custom list view by extending the base adapter to a custom one, but I am not able to implement a OnItemClickListener for the listview, as i want to show the Hotel Name of that row in Toast whenever the user clicks on a row of list view. I tried various example available on internet, but i just doesn't work.
Adapter
public class CustomListAdapterHotel extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<WorldsBillionaires> billionairesItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public CustomListAdapterHotel(Activity activity, List<WorldsBillionaires> billionairesItems) {
this.activity = activity;
this.billionairesItems = billionairesItems;
}
#Override
public int getCount() {
return billionairesItems.size();
}
#Override
public Object getItem(int location) {
return billionairesItems.get(location);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_hotel, null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
//NetworkImageView thumbNail = (NetworkImageView) convertView.findViewById(R.id.thumbnail);
TextView hotel_name = (TextView) convertView.findViewById(R.id.hotel_name);
TextView zone = (TextView) convertView.findViewById(R.id.zone);
TextView contact_person = (TextView) convertView.findViewById(R.id.contact_person);
TextView contact_number = (TextView) convertView.findViewById(R.id.contact_number);
TextView btc_direct = (TextView) convertView.findViewById(R.id.btcdirect);
// getting billionaires data for the row
WorldsBillionaires m = billionairesItems.get(position);
// name
hotel_name.setText(String.valueOf(m.getHotel_Name()));
zone.setText(String.valueOf(m.getHotel_Zone()));
contact_person.setText(String.valueOf(m.getContact_Person()));
contact_number.setText(String.valueOf(m.getContact_Number()));
btc_direct.setText(String.valueOf(m.getBtc_Direct()));
return convertView;
}
}
Model
public class WorldsBillionaires {
private String hotel_name,hotel_zone,contact_person,contact_number,btc_direct;
public WorldsBillionaires(String hotel_name, String hotel_zone, String contact_person, String contact_number, String btc_direct) {
this.hotel_name=hotel_name;
this.hotel_zone=hotel_zone;
this.contact_person=contact_person;
this.contact_number=contact_number;
this.btc_direct=btc_direct;
}
public WorldsBillionaires() {
}
public String getZone() {
return zone;
}
public void setZone(String zone) {
this.zone = zone;
}
public String getThumbnailUrl() {
return thumbnailUrl;
}
public void setThumbnailUrl(String thumbnailUrl) {
this.thumbnailUrl = thumbnailUrl;
}
public String getHotel_Name() {
return hotel_name;
}
public void setHotel_Name(String hotel_name) {
this.hotel_name = hotel_name;
}
public String getHotel_Zone() {
return hotel_zone;
}
public void setHotel_Zone(String hotel_zone) {
this.hotel_zone = hotel_zone;
}
public String getContact_Person() {
return contact_person;
}
public void setContact_Person(String contact_person) {
this.contact_person = contact_person;
}
public String getContact_Number() {
return contact_number;
}
public void setContact_Number(String contact_number) {
this.contact_number = contact_number;
}
public String getBtc_Direct() {
return btc_direct;
}
public void setBtc_Direct(String btc_direct) {
this.btc_direct = btc_direct;
}
}
Main Activity
public class ShowHotel extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
// Billionaires json url
private ProgressDialog pDialog;
private List<WorldsBillionaires> worldsBillionairesList = new ArrayList<WorldsBillionaires>();
private ListView listView;
private CustomListAdapterHotel adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_hotel);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapterHotel(this, worldsBillionairesList);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
// Creating volley request obj
JsonArrayRequest billionaireReq = new JsonArrayRequest("http://192.168.247.1/AdminBihar/getHotel.php?zone="+methods.zone,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
WorldsBillionaires worldsBillionaires = new WorldsBillionaires();
worldsBillionaires.setHotel_Name(obj.getString("hotel_name"));
worldsBillionaires.setThumbnailUrl(obj.getString("image"));
worldsBillionaires.setHotel_Zone(obj.getString("zone"));
worldsBillionaires.setContact_Person(obj.getString("contact_person"));
worldsBillionaires.setContact_Number(obj.getString("contact_number"));
worldsBillionaires.setBtc_Direct(obj.getString("btc_direct"));
// adding Billionaire to worldsBillionaires array
worldsBillionairesList.add(worldsBillionaires);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(billionaireReq);
}
#Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
}
so after you get json data, in activity that shows your list do something like this:
public class DisplayListView extends AppCompatActivity {
ListView listView;
protected void onCreate(){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_list_view);
listView = (ListView) findViewById(R.id.listview);
hotelAdapter = new CustomListAdapterHotel (this, R.layout.row_layout);
listView.setAdapter(hotelAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
Model stuff = (Model) hotelAdapter.getItem(position);
String hotelName = stuff.getHotel_Name();
Toast.makeText(getApplicationContext(), hotelName, Toast.LENGTH_SHORT).show();
}
});
there, this worked for me :)