gpt4 book ai didi

java - RecyclerView 在我第一次进入 Activity 时不填充,但在我退出并重新进入 Activity 后显示

转载 作者:太空宇宙 更新时间:2023-11-04 10:54:05 26 4
gpt4 key购买 nike

正如标题所描述的,我需要第一次进入 Activity 到空白屏幕。然后,我需要通过按返回退出 Activity 并再次重新进入 Activity 才能看到回收器 View 。幸运的是,当填充回收器 View 时,一切都井井有条,并且它准确地显示了我希望它在屏幕上显示的方式。

这是我的 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);

}
}

这是我的模型类:

  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,其中包含回收器 View :

 <?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 中设置回收器 View 以匹配父级,但它给了我相同的结果。正如您所看到的,我没有将 RecyclerView 嵌套在许多 View 中。难道是我在错误的时间设置了适配器,所以它给了我一个空列表?

最佳答案

“hosting_trip.xml”文件中的 recyclerview 有问题。您应该将 recyclerview 的高度更改为 match_parent (android:layout_height="match_parent")。

因为您尚未上传您的hosting_trip.xml 文件。您可以尝试让我知道解决方案。

关于java - RecyclerView 在我第一次进入 Activity 时不填充,但在我退出并重新进入 Activity 后显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47484712/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com