0

I can't retrieve data from the firebase and view it at recyclerView

this my database at firebaseenter image description here

There are no problem appear but in my run is not display the items this is my run enter image description here

and this my code I tried every thing I don't know what the problem help me please

public class account_preview extends AppCompatActivity {
private ArrayList<outflow>outflows;
private RecyclerView recyclerView;
RecyclerAdapter adapter;
DatabaseReference myRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_account_preview);

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    myRef = 
    database.getReference("user_account/(username)/bank_accounts/1");
    adapter = new RecyclerAdapter(outflows, account_preview.this);
    recyclerView = (RecyclerView) findViewById(R.id.RecyclerView);
    recyclerView.setHasFixedSize(true);
    LinearLayoutManager llm = new LinearLayoutManager(this);

    recyclerView.setLayoutManager(llm);
    llm.setOrientation(LinearLayoutManager.VERTICAL);

    onStart();

    new GetDataFromFirebase().
    executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

    // Read from the database
    recyclerView.setAdapter(adapter);

    }

    @Override
   protected void onStart() {
    super.onStart();
    myRef.child("outflow").child("1").addValueEventListener(new 
    ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            outflows=new ArrayList<outflow>();
            for (DataSnapshot dataSnapshot1: 
       dataSnapshot.getChildren()){
                outflow values =  
     dataSnapshot1.getValue(outflow.class);
                outflows.add(values);}



            adapter.notifyDataSetChanged();


        }

        @Override
        public void onCancelled(DatabaseError error) {
            // Failed to read value
            System.out.println("Failed to read value." + 
        error.toException());
        }
       });

      }

     private class GetDataFromFirebase extends 
     AsyncTask<Void,Void,Boolean>{

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Boolean doInBackground(Void... voids) {
        return false;
    }

    @Override
    protected void onPostExecute(Boolean aBoolean) {
        super.onPostExecute(aBoolean);
    }
    }
     }






  public class RecyclerAdapter extends 
  RecyclerView.Adapter<RecyclerAdapter.ViewHolder>{
      private Context context;

   private ArrayList<outflow> values;

   public RecyclerAdapter(ArrayList<outflow> values, account_preview 
  context) {
    this.values = values;
    this.context = context;
   }

  @Override
   public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
   {
    return new ViewHolder(LayoutInflater.from(parent.getContext())
            .inflate(R.layout.list_outflows, parent, false));


   }

  @Override
  public void onBindViewHolder(ViewHolder holder, int position) {

    holder.name.setText( values.get(position).getCategory());
    holder.c.setText((int) values.get(position).getAmount());
   }

  @Override
  public int getItemCount() {

    int arr = 0;

    try{
        if(values.size()==0){

            arr = 0;

        }
        else{

            arr=values.size();
        }



    }catch (Exception e){



    }

    return arr;

   }

public class ViewHolder extends RecyclerView.ViewHolder {
    private TextView name,c;
    ViewHolder(View itemView) {
        super(itemView);
        name = (TextView) itemView.findViewById(R.id.totaloutflow);
        c=(TextView)itemView.findViewById(R.id.total);
    }
    }
  }

public class outflow {

private double amount;
private String date;
private String time;
private String attachment;
private String category;
private String location;
private String vendor;
private int rate;


public outflow(double amount, String date, String time, String attachment, String category, String location, String vendor) {
    this.amount = amount;
    this.date = date;
    this.time = time;
    this.attachment = attachment;
    this.category = category;
    this.location = location;
    this.vendor = vendor;
}



public double getAmount() {
    return amount;
}

public void setAmount(double amount) {
    this.amount = amount;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}

public String getTime() {
    return time;
}

public void setTime(String time) {
    this.time = time;
}

public String getAttachment() {
    return attachment;
}

public void setAttachment(String attachment) {
    this.attachment = attachment;
}

public String getCategory() {
    return category;
}

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

public String getLocation() {
    return location;
}

public void setLocation(String location) {
    this.location = location;
}

public String getVendor() {
    return vendor;
}

public void setVendor(String vendor) {
    this.vendor = vendor;
}

public int getRate() {
    return rate;
}

public void setRate(int rate) {
    this.rate = rate;
}

}

activity_account_preview.xml
  <?xml version="1.0" encoding="utf-8"?>


   <android.support.constraint.ConstraintLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  >

    <android.support.v7.widget.RecyclerView
    android:id="@+id/RecyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


   </android.support.v7.widget.RecyclerView>
   </android.support.constraint.ConstraintLayout>




 list_outflows.xml



<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".account_preview">

  <android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="#27233A"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/navigation" />

   <TextView
    android:id="@+id/textView"
    android:layout_width="84dp"
    android:layout_height="44dp"
    android:layout_marginEnd="36dp"
    android:layout_marginStart="8dp"
    android:padding="5dp"
    android:text="(التاريخ)"
    android:textColor="#000000"
    android:textSize="20dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    tools:layout_editor_absoluteY="64dp"
    tools:ignore="MissingConstraints" />

  <Button
    android:id="@+id/button"
    android:layout_width="251dp"
    android:layout_height="61dp"
    android:layout_marginBottom="24dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:background="#9FB4C7"
    android:text="حذف الحساب"
    android:textColor="#ffffff"
    android:textSize="30dp"
    app:layout_constraintBottom_toTopOf="@+id/navigation"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.437"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView" />

  <android.support.design.widget.FloatingActionButton
    android:id="@+id/floatingActionButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="12dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:background="#D6C9C9"
    android:clickable="true"
    app:backgroundTint="#D6C9C9"
    app:layout_constraintBottom_toTopOf="@+id/navigation"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.976"
    app:layout_constraintStart_toStartOf="parent"
    app:srcCompat="@android:drawable/ic_input_add" />

  <android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_width="392dp"
    android:layout_height="64dp"
    android:layout_margin="2.5dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="184dp"
    app:cardBackgroundColor="@color/lightgrey"
    app:cardCornerRadius="4dp"
    app:layout_constraintBottom_toTopOf="@+id/button"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView"
    app:layout_constraintVertical_bias="1.0"
    tools:ignore="MissingConstraints">


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_weight="0.30"
        android:orientation="vertical">

        <TextView
            android:id="@+id/totaloutflow"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="4dp"
            android:text="الفواتير"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="25sp" />

        <TextView
            android:id="@+id/total"
            android:layout_width="388dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="4dp"
            android:layout_marginStart="4dp"
            android:text="ريال سعودي"
            android:textSize="15sp"
            android:textStyle="bold" />

    </LinearLayout>
    </android.support.v7.widget.CardView>

</android.support.constraint.ConstraintLayout>

2 Answers2

0

Firebase has its own Adapter for RecyclerView so better use that.

public FirebaseRecyclerAdapter<taskdata, ShowDataViewHolder> getmFirebaseAdapter() {
    return mFirebaseAdapter;
}

public void setmFirebaseAdapter(FirebaseRecyclerAdapter<taskdata, ShowDataViewHolder> mFirebaseAdapter) {
    this.mFirebaseAdapter = mFirebaseAdapter;
}


//View Holder For Recycler View
public static class ShowDataViewHolder extends RecyclerView.ViewHolder {
    private final TextView image_title  ;


    public ShowDataViewHolder(final View itemView)
    {
        super(itemView);

        image_title = itemView.findViewById(R.id.taskname);


    }

    private void Image_Title(String title)
    {
        image_title.setText(title);
    }


}


@Override
public void onStart() {
    super.onStart();

    FirebaseAuth mauth = FirebaseAuth.getInstance();
    String userod = mauth.getCurrentUser().getUid();
    myref = FirebaseDatabase.getInstance().getReference("Yourself").child(userod).child("task");

    setmFirebaseAdapter(new FirebaseRecyclerAdapter<taskdata, ShowDataViewHolder>(
            taskdata.class, R.layout.taskitem, ShowDataViewHolder.class, myref.orderByValue()) {
        public void populateViewHolder(final ShowDataViewHolder viewHolder, taskdata model, final int position) {

            viewHolder.Image_Title(model.getTaskname());


        }

    });


    recyclerView.setAdapter(getmFirebaseAdapter());


    myref.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if(dataSnapshot.getValue() == null){
                Toast.makeText(getContext(),"No Bookmarks added yet!",Toast.LENGTH_SHORT).show();
                }else {
                mFirebaseAdapter.notifyItemChanged(0);
                mFirebaseAdapter.notifyDataSetChanged();
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    mFirebaseAdapter.notifyItemInserted(taskdata2.size()-1);
}

If you need more help on the code please go to my github - https://github.com/afreakyelf/Yourself/blob/master/app/src/main/java/com/example/rajat/yourself/taskadapterhome.java

Rajat Mittal
  • 413
  • 5
  • 19
0

To solve this, please just add the following lines of code inside the costructor:

public RecyclerAdapter(List<outflow> list, account_preview context) {
    this.list = list
    this.context = context
}
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Are the items now displayed in your `RecyclerView`? For that error please see this [post](https://stackoverflow.com/questions/29141729/recyclerview-no-adapter-attached-skipping-layout). – Alex Mamo Feb 09 '19 at 11:30
  • Thanks but the items is not displayed – nora Khaled Feb 09 '19 at 12:01
  • In your `onBindViewHolder()` method, does `list.get(position) ` return an object of type `outflow`? – Alex Mamo Feb 09 '19 at 12:08
  • this is my onBindViewHolder() and yes it return outflow object @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.name.setText( values.get(position).getCategory()); holder.c.setText((int) values.get(position).getAmount()); } – nora Khaled Feb 09 '19 at 12:25
  • The error is disappear but still it not display the items I think the error is when I retrieve object from the firebase because I tray to retrieve one string and its run okay. – nora Khaled Feb 09 '19 at 12:31
  • If you are only trying to log that data, is it printing something in the logcat? – Alex Mamo Feb 10 '19 at 08:38
  • it retrieve object successfully but its not added to the list I don't know why – nora Khaled Feb 10 '19 at 18:33
  • Have you tried to comment this line `recyclerView.setHasFixedSize(true);`? Does it work? – Alex Mamo Feb 11 '19 at 07:50