1

As title describes, i need to enter the activity the first time to a blank screen. I then need to exit the activity by pressing back and re-enter the activity again to be able to see the recycler view. Fortunately, when the recycler view populates, everything is in order and it shows exactly how I want it to be shown on screen.

This is my activity class:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.hosting_trip);

    mOwnTripList = (RecyclerView) findViewById(R.id.host_trip_list);
    mOwnTripList.setHasFixedSize(true);
    mOwnTripList.setLayoutManager(new LinearLayoutManager(this));

 mPostIdRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            comPostArrayList.clear();
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                PostId postId = snapshot.getValue(PostId.class);
                    tripUniqueId = postId.getPostId();
                Log.d("$$$$$$$$$$$$$" , tripUniqueId);



        mLastRef = FirebaseDatabase.getInstance().getReference().child("comPostsCopy")
                   .child(tripUniqueId);

        mLastRef.addListenerForSingleValueEvent(new ValueEventListener() {
         @Override
         public void onDataChange(DataSnapshot dataSnapshot) {


         OwnHostTripItem ownHostTripPost = dataSnapshot
                                         .getValue(OwnHostTripItem.class);
              comPostArrayList.add(ownHostTripPost);
            Log.d("%%%%%",comPostArrayList.get(0).getTitle());

        }

         @Override
         public void onCancelled(DatabaseError databaseError) {

            }
         });

            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

     adapter = new YourHostAdapter( comPostArrayList , this.getApplicationContext());

    adapter.notifyDataSetChanged();

    mOwnTripList.setAdapter(adapter);

} 
  }

This is my model class :

  public class OwnHostTripItem {

String thumbPhoto;
String title;
String country;
String pricePerGuest;
String maxGroupSize;

public OwnHostTripItem() {

}

public OwnHostTripItem(String thumbPhoto, String title, String country, String pricePerGuest
                        , String maxGroupSize) {
    this.thumbPhoto = thumbPhoto;
    this.title = title;
    this.country = country;
    this.pricePerGuest = pricePerGuest;
    this.maxGroupSize = maxGroupSize;
}



public String getThumbPhoto() {
    return thumbPhoto;
}

public void setThumbPhoto(String thumbPhoto) {
    this.thumbPhoto = thumbPhoto;
}

public String getTitle() {
    return title;
}

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

public String getCountry() {
    return country;
}

public void setCountry(String country) {
    this.country = country;
}

public String getPricePerGuest() {
    return pricePerGuest;
}

public void setPricePerGuest(String pricePerGuest) {
    this.pricePerGuest = pricePerGuest;
}

public String getMaxGroupSize() {
    return maxGroupSize;
}

public void setMaxGroupSize(String maxGroupSize) {
    this.maxGroupSize = maxGroupSize;
}

}

This is my adapter:

 public class YourHostAdapter extends RecyclerView.Adapter<YourHostAdapter.ViewHolder> {

private ArrayList<OwnHostTripItem> ownHostTripList;
private Context context;

public YourHostAdapter(ArrayList<OwnHostTripItem> ownHostTripList, Context context) {
    this.ownHostTripList = ownHostTripList;
    this.context = context;
}

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

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    OwnHostTripItem ownHostTripListy = ownHostTripList.get(position);

    holder.mTripTitle.setText(ownHostTripListy.getTitle());
    holder.mTripCountry.setText(ownHostTripListy.getCountry());
    holder.mTripPrice.setText(ownHostTripListy.getPricePerGuest());
    holder.mTripSize.setText(ownHostTripListy.getMaxGroupSize());

    //For Image view
    Picasso.with(context).load(ownHostTripListy.getThumbPhoto())
    .placeholder(R.drawable.placeholder_image).into(holder.mTripImage)


}

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

public class ViewHolder extends RecyclerView.ViewHolder{

    View mView;
    public TextView mTripTitle;
    public TextView mTripCountry;
    public TextView mTripPrice;
    public TextView mTripSize;
    public ImageView mTripImage;

    public ViewHolder(View itemView) {
        super(itemView);

        mView = itemView;

        mTripTitle = (TextView) mView.findViewById(R.id.host_trip_title);
        mTripCountry = (TextView) mView.findViewById(R.id.host_trip_country);
        mTripPrice = (TextView) mView.findViewById(R.id.host_trip_price);
        mTripSize = (TextView) mView.findViewById(R.id.host_trip_size);

        mTripImage = (ImageView) mView.findViewById(R.id.host_trip_image);

          }
        }
       }

This is my xml with recycler view in it:

 <?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <include layout="@layout/app_bar_layout"
        android:id="@+id/hosting_trip_bar"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Oops, looks like you haven't been hosting any trips"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:id="@+id/hosting_trip_text"
        />


    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/host_trip_list"
        android:layout_below="@id/hosting_trip_bar"
        android:layout_marginTop="10dp" />


      </RelativeLayout>

I've tried setting adapter in onStart method and I've seen other post stating that we need to set the recycler view in xml to match parent but it gives me same result. As you can see, I've not nested my RecyclerView within many views. Could it be that I am setting the adapter at the wrong time so it gives me an empty list?

Hso Zote
  • 21
  • 8

2 Answers2

0

There is problem with the recyclerview in "hosting_trip.xml" file. You should change the height of the recyclerview to the match_parent (android:layout_height="match_parent").

As you have not uploaded your hosting_trip.xml file. You can try and let me know about the solution.

Ravi Basil
  • 31
  • 6
  • Sorry for not including my hosting_trip.xml file. I've editted my post and included it. However, I've already set the recyclerview height and width to match_parent since beginning. – Hso Zote Nov 25 '17 at 10:31
  • Did you get something in logcat when you run first time. – Ravi Basil Nov 25 '17 at 10:46
0

After searching for other posts, this link of combined answers from Ralph Bergman and Oussema Aroua solved my problem. What I needed to do in my activity is to create my adapter before I call the addListenerForSingleValueEvent method. After that, I need to set notifyDataSetChange and setAdapter after the for loop ends. Therefore, now my activity should look like this :

     @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.hosting_trip);

    mOwnTripList = (RecyclerView) findViewById(R.id.host_trip_list);
    mOwnTripList.setHasFixedSize(true);
    mOwnTripList.setLayoutManager(new LinearLayoutManager(this));

 mPostIdRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            comPostArrayList.clear();
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                PostId postId = snapshot.getValue(PostId.class);
                    tripUniqueId = postId.getPostId();
                Log.d("$$$$$$$$$$$$$" , tripUniqueId);


    adapter = new YourHostAdapter( 
             comPostArrayList,this.getApplicationContext());

        mLastRef = FirebaseDatabase.getInstance().getReference().child("comPostsCopy")
                   .child(tripUniqueId);

        mLastRef.addListenerForSingleValueEvent(new ValueEventListener() {
         @Override
         public void onDataChange(DataSnapshot dataSnapshot) {


         OwnHostTripItem ownHostTripPost = dataSnapshot
                                         .getValue(OwnHostTripItem.class);
              comPostArrayList.add(ownHostTripPost);
            Log.d("%%%%%",comPostArrayList.get(0).getTitle());

        }

         @Override
         public void onCancelled(DatabaseError databaseError) {

            }
         });

            }
         adapter.notifyDataSetChanged();

         mOwnTripList.setAdapter(adapter);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });





} 
  }
Hso Zote
  • 21
  • 8