0

제목에서 설명한대로 처음에는 빈 화면에 활동을 입력해야합니다. 그런 다음 다시 눌러 활동을 종료하고 재활용 자보기를 볼 수 있도록 활동을 다시 입력해야합니다. 다행스럽게도, 리사이클 러 뷰가 채워지면 모든 것이 순서대로 표시되고 정확히 어떻게 화면에 표시 할 것인지를 보여줍니다.RecyclerView는 활동을 처음 입력 할 때 표시되지 않지만 나가서 나가서 활동을 다시 입력하면 표시됩니다.

이 내 어댑터입니다

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

} : 이것은 내 모델 클래스입니다

@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 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를 여러보기에 중첩하지 않았습니다. 어댑터를 잘못된 시간에 설정하여 빈 목록을 제공 할 수 있습니까?

답변

0

다른 게시물을 검색 한 후 this Ralph Bergman과 Oussema Aroua의 결합 된 답변 링크가 내 문제를 해결했습니다. 내 활동에서해야 할 일은 addListenerForSingleValueEvent 메소드를 호출하기 전에 어댑터를 만드는 것입니다. 그런 다음 for 루프가 끝난 후에 notifyDataSetChange 및 setAdapter를 설정해야합니다. 따라서 내 활동은 다음과 같아야합니다.

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

     } 
    }); 





} 
    } 
0

"hosting_trip.xml"파일의 recyclerview에 문제가 있습니다. recyclerview의 높이를 match_parent (android : layout_height = "match_parent")로 변경해야합니다.

hosting_trip.xml 파일을 업로드하지 않으 셨습니다. 해결책에 대해 알려 주시면 감사하겠습니다.

+0

내 hosting_trip.xml 파일을 포함하지 않아서 죄송합니다. 나는 내 게시물을 편집하고 그것을 포함 시켰습니다. 그러나 저는 이미 recyclerview 높이와 너비를 match_parent로 설정했습니다. –

+0

처음 실행했을 때 logcat에서 뭔가를 얻었습니까? –