0

Im working on getting data from firebase, though nothing happens, blank page. I get Toast message, that It failed, but I don't get data. I've got notification 'E/RecyclerView: No adapter attached; skipping layout firebase'

How to solve it?

Model

Planner.cpp

public class Planner {

// Variable to store data corresponding
// to firstname keyword in database
private String title, category,startHour,startMinute,duration,text, Month, Day, Year;

public String getTitle() {
    return title;
}

public String getCategory() {
    return category;
}

public String getStartHour() {
    return startHour;
}

public String getStartMinute() {
    return startMinute;
}

public String getDuration() {
    return duration;
}

public String getText() {
    return text;
}

public String getMonth() {
    return Month;
}

public String getDay() {
    return Day;
}

public String getYear() {
    return Year;
}

public Planner() {
}

public Planner(String title, String category, String startHour, String startMinute, String duration, String text, String month, String day, String year) {
    this.title = title;
    this.category = category;
    this.startHour = startHour;
    this.startMinute = startMinute;
    this.duration = duration;
    this.text = text;
    Month = month;
    Day = day;
    Year = year;
}

public void setTitle(String title) {
    this.title = title;
}

public void setCategory(String category) {
    this.category = category;
}

public void setStartHour(String startHour) {
    this.startHour = startHour;
}

public void setStartMinute(String startMinute) {
    this.startMinute = startMinute;
}

public void setDuration(String duration) {
    this.duration = duration;
}

public void setText(String text) {
    this.text = text;
}

public void setMonth(String month) {
    Month = month;
}

public void setDay(String day) {
    Day = day;
}

public void setYear(String year) {
    Year = year;
}
// Mandatory empty constructor
    // for use of FirebaseUI

}

Managment.cpp

public class Managment extends AppCompatActivity {

private TextView mTv;
private Button mBtn;
private Calendar c;
private DatePickerDialog dpd;

//Firebase recyclerview
DatabaseReference ref;
RecyclerView recyclerView;
ArrayList<Planner> list;
MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_managment);
    mTv = (TextView)findViewById(R.id.txtView);
    mBtn = (Button)findViewById(R.id.btnPick);

    //Firebase variables
    //RetrievePlans = (RecyclerView)findViewById(R.id.retrievedata);



    mBtn.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view)
        {
            c = Calendar.getInstance();
            int day = c.get(Calendar.DAY_OF_MONTH);
            int month =c.get(Calendar.MONTH);
            int year = c.get(Calendar.YEAR);

            dpd = new DatePickerDialog(Managment.this, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker datePicker, int mDay, int mMonth, int mYear) {
                    mTv.setText(mDay + "/" + (mMonth + 1) + "/" + mYear);

                }
            },day, month, year);
            dpd.show();
    }
});
    recyclerView = findViewById(R.id.retrievedata);
    recyclerView.setVisibility(View.GONE);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    list = new ArrayList<Planner>();
    ref =FirebaseDatabase.getInstance().getReference().child("Planer");
    ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            for(DataSnapshot dataSnapshotl: snapshot.getChildren()) {
                Planner p = dataSnapshotl.getValue(Planner.class);
                list.add(p);
            }
            adapter=new MyAdapter(Managment.this,list);
            recyclerView.setAdapter(adapter);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
            Toast.makeText(Managment.this, "Opps something went wrong", Toast.LENGTH_SHORT).show();
        }
    });





}

}

I need help with this. This is how data looks like in firestore enter image description here

@edit

MyAdapter.java

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

Context context;
ArrayList<Planner> plans;
public MyAdapter(Context c, ArrayList<Planner> p){
    context=c;
    plans = p;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.object,parent,false));
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    holder.textViewtitle.setText(plans.get(position).getTitle());
    holder.textViewCategory.setText(plans.get(position).getCategory());
    holder.textViewtext.setText(plans.get(position).getText());
    holder.textViewduration.setText(plans.get(position).getStartHour()+":"+plans.get(position).getStartMinute()+"lasts"+plans.get(position).getDuration()+"minutes");



}


@Override
public int getItemCount() {
    return plans.size();
}

class MyViewHolder extends RecyclerView.ViewHolder
{
    TextView textViewtitle, textViewduration,textViewtext, textViewCategory;

    public MyViewHolder(@NonNull View itemView) {
        super(itemView);
        textViewtitle=itemView.findViewById(R.id.title);
        textViewduration=itemView.findViewById(R.id.duration);
        textViewtext=itemView.findViewById(R.id.insides);
        textViewCategory=itemView.findViewById(R.id.category);


    }
}

}

David Innocent
  • 606
  • 5
  • 16

1 Answers1

0

The proper way of instantiating an adapter is as follows.

Try

List<Planner> plannerList=new ArrayList<>();

Then on onCreate() Initialize your adapter like the following

adapter=new MyAdapter(list);
recyclerView = findViewById(R.id.retrievedata);
    recyclerView.setVisibility(View.GONE);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);

Don't pass context like that also you can get the context like the following code.

Context context;
List<Planner> plans;
public MyAdapter(List<Planner> plans){
    this.plans = plans;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
context=parent.getContext()
    return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.object,parent,false));
}

After you have finished with the above the only thing you need to do is fetch the list of planners from firebase the add them to the plannerList like the following.

plannersList.add(planner)

Then outside the for loop call.

adapter.notifyDataSetChanged()
David Innocent
  • 606
  • 5
  • 16