I'm developing simple project. CategoryFragment is the first added fragment in MainActivity. I expect receive data from FireBase and display it with RecyclerView.
Below my code. The app doesn't fall during launching. There is no any error.
I guess I should use some method for my purpose, but I'm new in Firebase.
public class CategoryFragment extends Fragment {
public DatabaseReference mRootDB;
private RecyclerView mRecyclerView;
public FirebaseRecyclerAdapter<Category, CategoryViewHolder> adapter;
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mRootDB = FirebaseDatabase.getInstance().getReference().child("categories");
adapter = new FirebaseRecyclerAdapter <Category, CategoryViewHolder>(
Category.class,
R.layout.model_category,
CategoryViewHolder.class,
mRootDB)
{
#Override
protected void populateViewHolder(CategoryViewHolder viewHolder, Category model, int position) {
viewHolder.setCategory(model.getName());
}
};
mRecyclerView.setAdapter(adapter);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.frag_category, container, false);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.category_RV);
return rootView;
}
public static class CategoryViewHolder extends RecyclerView.ViewHolder {
View mView;
TextView category_CV;
public CategoryViewHolder(View itemView) {
super(itemView);
mView = itemView;
category_CV = (TextView) mView.findViewById(R.id.category_CV);
}
public void setCategory(String data){
category_CV.setText(data);
}
}
}
I would not override onActivityCreated() if I were you, you can create and return the RecyclerView in the onCreateView(). Are you sure you have data in Firebase? You can override parseSnapshot() like this and maybe put a break point to make sure that something is coming back, and if it does then you may want to check your RecyclerView custom layout.
public class CategoryFragment extends Fragment {
public DatabaseReference mRootDB;
private RecyclerView mRecyclerView;
public FirebaseRecyclerAdapter<Category, CategoryViewHolder> adapter;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.frag_category, container, false);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.category_RV);
mRootDB = FirebaseDatabase.getInstance().getReference().child("categories");
adapter = new FirebaseRecyclerAdapter <Category, CategoryViewHolder>(
Category.class,
R.layout.model_category,
CategoryViewHolder.class,
mRootDB)
{
#Override
protected Category parseSnapshot(DataSnapshot snapshot) {
Category category = super.parseSnapshot(snapshot);
if (category != null){
category.setId(snapshot.getKey());
}
return category;
}
#Override
protected void populateViewHolder(CategoryViewHolder viewHolder, Category model, int position) {
viewHolder.setCategory(model.getName());
}
};
mRecyclerView.setAdapter(adapter);
return rootView;
}
public static class CategoryViewHolder extends RecyclerView.ViewHolder {
View mView;
TextView category_CV;
public CategoryViewHolder(View itemView) {
super(itemView);
mView = itemView;
category_CV = (TextView) mView.findViewById(R.id.category_CV);
}
public void setCategory(String data){
category_CV.setText(data);
}
}
}
Related
I am writing a app which use a recycleView in fragement. However only the textView in that fragement show,there is nothing in the recycleView ,function like onBindViewHolder() in the recycleView Adapter wasn't been called .Here is my code of recycleViewAdapter and fragement.
Adapter
import java.util.List;
public class ShoppingAdapter extends RecyclerView.Adapter<ShoppingAdapter.MyViewHolder> {
private List<Shopping> localData;
public ShoppingAdapter(List<Shopping> localData) {
this.localData = localData;
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView textView;
ImageView imageView;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
textView =itemView.findViewById(R.id.shopping_text);
imageView = itemView.findViewById(R.id.shopping_img);
}
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
Log.d("test", "onCreateViewHolder: ");
View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.shopping_item,parent,false);
return null;
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
holder.textView.setText(localData.get(position).getTitle());
holder.imageView.setImageResource(localData.get(position).getImageResource());
}
#Override
public int getItemCount() {
return this.localData.size();
}
}
Fragment
public class shoppingFragment extends Fragment {
View view;
List<Shopping> shoppingList;
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initShoppingList();
RecyclerView recyclerView =(RecyclerView) view.findViewById(R.id.shopping_recycle_view);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(view.getContext());
ShoppingAdapter myAdapter=new ShoppingAdapter(shoppingList);
recyclerView.setAdapter(myAdapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_shopping, container, false);
return view;
}
public void initShoppingList(){
shoppingList = new ArrayList<Shopping>();
shoppingList.add(new Shopping(R.drawable.data,"data"));
shoppingList.add(new Shopping(R.drawable.position,"position"));
shoppingList.add(new Shopping(R.drawable.floor,"floor"));
}
}
Looks like you are missing 2 crucial things.
onCreateViewHolder should return a view. You return null.
return new MyViewHolder(view);
You forgot to set the LayoutManager to your adapter.
recyclerView.setLayoutManager(linearLayoutManager);
Instead of returning null at onCreateViewHolder, you should return new MyViewHolder(view);
And you forgot to assign the layoutmanager you created on your fragment into your recyclerview (recyclerView.setLayoutManager(linearLayoutManager);)
Also, you don't need to cast recyclerview to recyclerview. You can remove (RecyclerView) from RecyclerView recyclerView =(RecyclerView) view.findViewById(R.id.shopping_recycle_view);
This question already has answers here:
How to implement Firebase Recycler Adapter in newer version of Android 3.1 and higher?
(2 answers)
Closed 3 years ago.
So, I watched some tutorial on how to do the RecyclerView mainly this. I tweaked the code to fit my needs. But when i try to ran it, the RecyclerView isn't displaying on the fragment. My guess is the problem lies in adapter and viewholder part, because i'm not sure how to implement it yet. Can anyone help me?
The Fragment.java
public class JobListFragment extends Fragment {
RecyclerView mRecyclerView;
FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;
public JobListFragment(){
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
((MainHomeActivity)getActivity()).setActionBarTitle("Job List");
View view = inflater.inflate(R.layout.fragment_job_list, container, false);
firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = firebaseDatabase.getReference().child("Data");
mRecyclerView = view.findViewById(R.id.recycleView);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mRecyclerView.setHasFixedSize(true);
return view;
}
#Override
public void onStart() {
super.onStart();
FirebaseRecyclerOptions<Model> options =
new FirebaseRecyclerOptions.Builder<Model>()
.setQuery(databaseReference, Model.class).build();
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Model, ViewHolder>(options) {
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.row, viewGroup, false);
return new ViewHolder(view);
}
#Override
protected void onBindViewHolder(#NonNull ViewHolder holder, int position, #NonNull Model model) {
holder.setDetails(model.getTitlept(), model.getDescpt(), model.getImagept());
}
};
mRecyclerView.setAdapter(adapter);
}
}
ViewHolder.java
public class ViewHolder extends RecyclerView.ViewHolder {
View mView;
public ViewHolder(View itemView){
super(itemView);
mView = itemView;
}
public void setDetails(String title, String description, String image){
TextView mTitleTv = mView.findViewById(R.id.rTitleTv);
TextView mDescTv = mView.findViewById(R.id.rDescIv);
ImageView mImageIv = mView.findViewById(R.id.rImageIv);
mTitleTv.setText(title);
mDescTv.setText(description);
Picasso.get().load(image).into(mImageIv);
}
}
Model.java
public class Model {
String titlept, imagept, descpt;
public Model(){}
public String getTitlept() {
return titlept;
}
public void setTitlept(String titlept) {
this.titlept = titlept;
}
public String getImagept() {
return imagept;
}
public void setImagept(String imagept) {
this.imagept = imagept;
}
public String getDescpt() {
return descpt;
}
public void setDescpt(String descpt) {
this.descpt = descpt;
}
}
Can you write the problem a little bit more clearly? Picture not displayed? #AdhityoPutro
I want to grey out fragment activity on some condition, I am trying to set a flag in pojo class and checking every time i try to grey out an activity.
I do not know where exactly the logic should be placed. I tried to put it inside a onCreateView() method, but it gave me an null pointer exception.
Please find the code below for your reference.
#SuppressLint("ValidFragment")
public class QuizTab extends Fragment {
private static final String TAG = "QuizTab";
RecyclerView recyclerView;
View view;
LinearLayout linearLayout;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.quiztab, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
if(flag){
grey out logic ---- not working
}
getQuizContent();
setupRecyclerView(recyclerView);
return view;
}
private void setupRecyclerView(RecyclerView recyclerView) {
recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
recyclerView.setAdapter(new SimpleStringRecyclerViewAdapter(getActivity()));
}
public class SimpleStringRecyclerViewAdapter extends RecyclerView.Adapter<SimpleStringRecyclerViewAdapter.ViewHolder> {
private Context mContext;
public class ViewHolder extends RecyclerView.ViewHolder {
public final LinearLayout mLinearlayout;
public ViewHolder(View view) {
super(view);
mLinearlayout = (LinearLayout) view.findViewById(R.id.mcq);
}
}
public SimpleStringRecyclerViewAdapter(Context context) {
mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.quiztab_mcq, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
}
#Override
public int getItemCount() {
return 1;
}
}
}
LinearLayout in this case is parent of fragment i tryed to paint fragment but failure so i did something like that
if(flag){
LinearLayout layout = view.findViewById(R.id.parentOffragment);
layout.setBackgroundColor(Color.GRAY);
}
I'm learning android and I'm just new to this concept.
This is my adapter class. It shows an error saying that the CategoryList is not an enclosing class. I'm unable to understand the error. Please help me in uderstanding it. Thanks in advance.
public class CategoryAdapter extends RecyclerView.Adapter<CategoryHolder> {
private final Context context;
private final List<Category> categories;
public CategoryAdapter(Context context, List<Category> categories) {
this.context = context;
this.categories = categories;
}
#Override
public CategoryHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
// Here it's showing an error: CategoryList is not an enclosing class.
return new CategoryHolder(layoutInflater, parent);
}
#Override
public void onBindViewHolder(CategoryHolder holder, int position) {
final Category category = categories.get(position);
holder.bind(category);
}
#Override
public int getItemCount() {
return categories.size();
}
}
This is my first fragment. It contains the list of categories such as google, facebook etc. On click on any of these items, it should open another fragment with some detail regarding the category.
public class CategoryList extends Fragment {
private RecyclerView recyclerView;
public CategoryList() { }
private void updateUI() {
List<Category> categories = new ArrayList<>();
categories.add(new Category(1L, "Google", "Hello world!! This is Google."));
categories.add(new Category(2L, "Facebook", "Hello world!! This is Facebook"));
categories.add(new Category(3L, "WhatsApp", "Hello world!! This is WhatsApp"));
categories.add(new Category(4L, "LinkedIn", "Hello world!! This is LinkedIn"));
CategoryAdapter categoryAdapter = new CategoryAdapter(getActivity(), categories);
recyclerView.setAdapter(categoryAdapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_category_list, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.category_recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
updateUI();
return view;
}
public class CategoryHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private Category category;
private final TextView name;
private final TextView description;
public CategoryHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.category_layout, parent, false));
this.name = (TextView) itemView.findViewById(R.id.category_name);
this.description = (TextView) itemView.findViewById(R.id.category_description);
}
public void bind(final Category category) {
this.category = category;
this.name.setText(category.getName());
this.description.setText(category.getDescription());
}
#Override
public void onClick(View v) {
Fragment fragment = CategoryDetail.getInstance(category);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
fragmentManager.beginTransaction()
.add(R.id.detail_container, fragment)
.commit();
}
}
}
This is the fragment that contains the detail of the category. For now I've not written anything in updateUI() method.
public class CategoryDetail extends Fragment {
private Category category;
public CategoryDetail() { }
public static CategoryDetail getInstance(Category category) {
CategoryDetail categoryDetail = new CategoryDetail();
categoryDetail.category = category;
return categoryDetail;
}
private void updateUI() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.category_detail, container, false);
updateUI();
return view;
}
}
You didn't create a ViewHolder class. Based on your code, you would want to create
public class CategoryHolder extends RecyclerView.ViewHolder {
public CategoryHolder(View view) {
//code for the constructor for the ViewHolder goes here.
}
}
I have referred quite few posts regarding this error but none of them could able to solve my issue. I have a fragment where I get my values using retrofit which I have shown below:
public class Tab extends Fragment
{
public String nlink;
public RecyclerView recycler;
public List<Initialise> item;
public Adapter adapter;
public Context mContext;
public Tab(Context mContext)
{
this.mContext=mContext;
}
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState)
{
View v= inflater.inflate(R.layout.tab_layout,container,false);
item = new ArrayList<>();
recycler=(RecyclerView)v.findViewById(R.id.recycler);
recycler.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(getContext());
llm.setOrientation(LinearLayoutManager.VERTICAL);
recycler.setLayoutManager(llm);
recycler.setItemAnimator(new DefaultItemAnimator());
adapter = new Adapter(item,mContext);
recycler.setAdapter(adapter);
return v;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
Bundle bundle=getArguments();
nlink=bundle.getString("link");
Retrofit adapter = new Retrofit.Builder()
.baseUrl(ItemAdapter.ROOT_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
Service Serviceapi = adapter.create(Service.class);
Serviceapi.getitem(nlink).enqueue(new Callback<Item>()
{
#Override
public void onResponse(Call<Item> call, Response<Item> response)
{
for(int i=0;i<response.body().getitems().size();i++)
{
item.add(new Initialise(response.body().getitems().get(i).getTitle(),response.body().getitems().get(i).getDate(),response.body().getitems().get(i).getcount(),response.body().getitems().get(i).getImage()));
}
adapter.notifyDataSetChanged();
}
#Override
public void onFailure(Call<Item> call, Throwable t)
{
}
});
}
}
And the following is my RecyclerView Adapter class:
public class Adapter extends RecyclerView.Adapter<Adapter.Holder>
{
static class Holder extends RecyclerView.ViewHolder
{
CardView cv;
ImageView thum_img;
TextView thum_time;
TextView thum_count;
TextView thum_text;
Holder(View itemView)
{
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);
thum_img = (ImageView)itemView.findViewById(R.id.thum_img);
thum_time = (TextView)itemView.findViewById(R.id.thum_time);
thum_count = (TextView)itemView.findViewById(R.id.thum_scount);
thum_text = (TextView)itemView.findViewById(R.id.thum_text);
}
}
private List<Initialise> item;
private Context mContext;
public Adapter(List<Initialise> item, Context mContext)
{
this.item=item;
this.mContext=mContext;
}
#Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType)
{
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.tab_layout, parent, false);
return new Holder(v);
}
#Override
public void onBindViewHolder(Holder holder, int position)
{
holder.thum_text.setText(item.get(position).gettitle());
holder.thum_time.setText(item.get(position).gettime());
holder.thum_count.setText(item.get(position).getcount());
Picasso.with(mContext).load(item.get(position).getstoryImage()).resize(120, 60).into(holder.thum_img);
}
#Override
public int getItemCount()
{
return item.size();
}
}
I'm getting the following error:
E/RecyclerView: No adapter attached; skipping layout
My cardview is not getting displayed and the viewpager is empty. Where am I going wrong ?
Try to edit onCreateViewHolder() method
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.tab_layout,parent,false);
Holder rcv = new Holder(layoutView,mContext);
return rcv;