タイトルが説明するように、初めて空白の画面にアクティビティを入力する必要があります。その後、戻るボタンを押してアクティビティーを終了し、アクティビティーを再入力して、リサイクル・ビューを表示できるようにする必要があります。幸運なことに、リサイクラビューが表示されると、すべてが順番に表示され、画面に表示する方法を正確に示します。RecyclerViewは最初に入力されませんが、アクティビティを終了した後に表示されます。
これが私の活動のクラスです:
@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:
<?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を多くのビューの中にネストしていません。間違った時間にアダプタを設定して空リストにすることができますか?
私のhosting_trip.xmlファイルを含めないと申し訳ありません。私は私の投稿を編集し、それを含めました。しかし、私はすでにrecyclerviewの高さと幅を最初からmatch_parentに設定しています。 –
最初に実行したときにlogcatで何かを取得しましたか? –