RecyclerView不会在我第一次进入活动时填充,而是在退出并重新进入活动后显示 [英] RecyclerView not populating the first time I enter the activity but showing after i exit and re-enter the activity

查看:66
本文介绍了RecyclerView不会在我第一次进入活动时填充,而是在退出并重新进入活动后显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所述,我需要在第一次进入空白屏幕时输入活动.然后,我需要通过回退退出活动并再次重新进入活动,以便能够看到回收站视图.幸运的是,当填充回收器视图时,一切都井井有条,并且可以准确显示我希望在屏幕上显示的方式.

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.

这是我的活动课程:

 @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);

} 
  }

这是我的模型课:

  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;
}

}

这是我的适配器:

 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);

          }
        }
       }

这是我的带有回收器视图的xml:

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>

我尝试过在onStart方法中设置适配器,并且我看到其他文章指出我们需要在xml中设置回收站视图以匹配父项,但是它给了我相同的结果.如您所见,我没有将RecyclerView嵌套在许多视图中.可能是我在错误的时间设置了适配器,这样它给了我一个空的清单吗?

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?

推荐答案

搜索其他帖子后,

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) {

        }
    });





} 
  }

这篇关于RecyclerView不会在我第一次进入活动时填充,而是在退出并重新进入活动后显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆