2017-04-18 44 views
0

私のコードでカードを表示する際にエラーが見つかりません。 Firebaseを使用してカードにデータを描画していますが、これまではgroup_nameを最初に表示しようとしています。CardViewが表示されません

Hot.java活性

private RecyclerView recyclerViews; 
    private StorageReference mStorageRef; 
    private FirebaseAuth mAuth; 
    private FirebaseAuth.AuthStateListener mAuthListener; 

    List<Groups> list; 

    FirebaseDatabase database = FirebaseDatabase.getInstance(); 
    DatabaseReference myRef = database.getReference("Users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()); 

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

     recyclerViews = (RecyclerView)findViewById(R.id.recyclerView); 

     new GetDataFromFirebase().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 
     DatabaseReference test = database.getReference("Groups"); 
     ValueEventListener valueEventListener = test.addValueEventListener(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       list = new ArrayList<Groups>(); 
       for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) { 
        System.out.println(dataSnapshot1.getValue()); 

        Groups value = dataSnapshot1.getValue(Groups.class); 
        Groups fire = new Groups(); 
        String name = value.getGroupName(); 
        String bannerImage = value.getGroupBannerImage(); 
        String iconImage = value.getGroupIcon(); 
        String description = value.getGroupDescription(); 
        String dateCreated = value.getGroupCreatedDate(); 
        Boolean NSFW = value.getGroupNSFW(); 
        Boolean openPublic = value.getGroupOpenPublic(); 
        Integer followers = value.getGroupNoOfFollowers(); 
        Integer posts = value.getGroupNoOfPosts(); 

        fire.setGroupName(name); 
        //fire.setGroupBannerImage(bannerImage); 
        //fire.setGroupIcon(iconImage); 
        //fire.setGroupDescription(description); 
        //fire.setGroupCreatedDate(dateCreated); 
        //fire.setGroupNSFW(NSFW); 
        //fire.setGroupOpenPublic(openPublic); 
        //fire.setGroupNoOfFollowers(followers); 
        //fire.setGroupNoOfPosts(posts); 

        Toast.makeText(Hot.this, fire.group_name , Toast.LENGTH_LONG).show(); 
        list.add(value); 

       } 
      } 


      @Override 
      public void onCancelled(DatabaseError error) { 
       // Failed to read value 
       System.out.println("Failed to read value." + error.toException()); 
      } 


     }); 

     RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(list, Hot.this); 
     //RecyclerView.LayoutManager recyce = new GridLayoutManager(Hot.this, 2); 
     RecyclerView.LayoutManager recyce = new LinearLayoutManager(Hot.this); 
     //recyclerViews.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true)); 
     recyclerViews.setLayoutManager(recyce); 
     recyclerViews.setItemAnimator(new DefaultItemAnimator()); 
     recyclerViews.setAdapter(recyclerViewAdapter); 

RecyclerViewAdapter.java

> package com.example.peace.eat; 
> 
> import android.content.Context; import 
> android.support.v7.widget.RecyclerView; import 
> android.view.LayoutInflater; import android.view.View; import 
> android.view.ViewGroup; import android.widget.TextView; 
> 
> import java.util.List; 
> 
> class RecyclerViewAdapter extends 
> RecyclerView.Adapter<RecyclerViewAdapter.MyHoder> { 
>  List<Groups> list; 
>  Context context; 
> 
>  public RecyclerViewAdapter(List<Groups> list, Context context) { 
>   this.list = list; 
>   this.context = context; 
>  } 
> 
>  @Override 
>  public MyHoder onCreateViewHolder(ViewGroup parent, int viewType) { 
>   View view = LayoutInflater.from(context).inflate(R.layout.recycler_view_groups,parent,false); 
>   MyHoder myHoder = new MyHoder(view); 
>   return myHoder; 
>  } 
> 
>  @Override 
>  public void onBindViewHolder(MyHoder holder, int position) { 
>   Groups mylist = list.get(position); 
>   holder.name.setText(mylist.getGroupName()); 
>   //holder.email.setText(mylist.getGroupNoOfPosts()); 
>   //holder.address.setText(mylist.getAddress()); 
>  } 
> 
>  @Override 
>  public int getItemCount() { 
>   int arr = 0; 
>   try{ 
>    if(list.size()==0){ 
>     arr = 0; 
>    } 
>    else{ 
>     arr=list.size(); 
>    } 
>   }catch (Exception e){ 
> 
>   } 
>   return arr; 
>  } 
> 
>  class MyHoder extends RecyclerView.ViewHolder{ 
>   TextView name,email,address; 
> 
>   public MyHoder(View itemView) { 
>    super(itemView); 
>    name = (TextView) itemView.findViewById(R.id.groupName); 
>    //email= (TextView) itemView.findViewById(R.id.vemail); 
>    //address= (TextView) itemView.findViewById(R.id.vaddress); 
> 
>   } 
>  } } 

Groups.java

public class Groups { 
    public String group_name; 
    public String group_banner_url; 
    public String group_icon_url; 
    public String group_description; 
    public String date_created; 
    public Boolean nsfw; 
    public Boolean open_public; 
    public Integer no_of_followers; 
    public Integer no_of_posts; 



    public String getGroupName() { 
     return group_name; 
    } 
    public void setGroupName(String groupName){ 
     this.group_name = groupName; 
    } 

    public String getGroupBannerImage() { 
     return group_banner_url; 
    } 
    public void setGroupBannerImage(String groupBannerImage) { 
     this.group_banner_url = groupBannerImage; 
    } 

    public Boolean getGroupNSFW() { 
     return nsfw; 
    } 
    public void setGroupNSFW(Boolean groupNSFW) { 
     this.nsfw = groupNSFW; 
    } 

    public String getGroupIcon() { 
     return group_icon_url; 
    } 
    public void setGroupIcon(String groupIcon) { 
     this.group_icon_url = groupIcon; 
    } 

    public String getGroupDescription() { 
     return group_description; 
    } 
    public void setGroupDescription(String groupDescription) { 
     this.group_description = groupDescription; 
    } 

    public String getGroupCreatedDate() { 
     return date_created; 
    } 
    public void setGroupCreatedDate(String groupCreatedDate) { 
     this.date_created = groupCreatedDate; 
    } 

    public Boolean getGroupOpenPublic() { 
     return open_public; 
    } 
    public void setGroupOpenPublic(Boolean groupOpenPublic) { 
     this.open_public = groupOpenPublic; 
    } 

    public Integer getGroupNoOfFollowers() { 
     return no_of_followers; 
    } 
    public void setGroupNoOfFollowers(Integer getGroupNoOfFollowers) { 
     this.no_of_followers = getGroupNoOfFollowers; 
    } 

    public Integer getGroupNoOfPosts() { 
     return no_of_posts; 
    } 
    public void setGroupNoOfPosts(Integer getGroupNoOfPosts) { 
     this.no_of_posts = getGroupNoOfPosts; 
    } 
} 

recycler_view_groups.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/recycler_item" 
    android:layout_width="match_parent" 
    android:layout_height="175dp" 
    android:layout_margin="10dp" 
    android:clickable="true" 
    android:foreground="?android:attr/selectableItemBackground" 
    android:scaleType="centerCrop" 
    card_view:cardCornerRadius="4dp" 
    card_view:cardElevation="6dp" 
    card_view:contentPadding="0dp"> 

    <ImageView 
     android:id="@+id/groupBannerImage" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_margin="0dp" 
     android:layout_weight="0" 
     android:padding="0dp" 
     android:src="@drawable/hehe" 
     card_view:srcCompat="@drawable/common_google_signin_btn_icon_dark" /> 

    <TextView 
     android:id="@+id/groupName" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_gravity="bottom" 
     android:layout_weight="1" 
     android:gravity="bottom" 
     android:minLines="4" 
     android:paddingBottom="10dp" 
     android:paddingLeft="10dp" 
     android:textAlignment="viewStart" 
     android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" 
     android:textSize="36sp" 
     android:textStyle="bold" /> 

</android.support.v7.widget.CardView> 

My Firebase Data

私recyclerviewは全く何も示さないように私が欠けているか間違ってやっているものがあります。

+0

リストに値を追加するときは、recyclerviewAdapter.notifyDatasetChanged();を呼び出します。 notifyDatsetChangedの前にアダプタがnullかどうかを確認する必要があります。 –

+0

recyclerViews.getAdapter()。getItemCount()は0を返しますが、リストに追加するときはlist.size ()は1を返します。これは私のRecyclerViewAdapterのロジックが間違っていることを意味しますか? –

+0

adapter.getItemCount()が正常に機能していれば、リストサイズの項目の数を返します.Adapterクラスでそのメソッドをオーバーライドしています。 –

答えて

0

ネットで検索した後、私はRecyclerViewをonCreateメソッドに配置する代わりに、自分のリストが完全に追加された後に呼び出されるメソッドの中にすべてのRecyclerViewを配置する必要があることを発見しました。

Hot.java活動

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

     recyclerViews = (RecyclerView)findViewById(R.id.recyclerView); 

     new GetDataFromFirebase().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 
     DatabaseReference test = database.getReference("Groups"); 
     ValueEventListener valueEventListener = test.addValueEventListener(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       list = new ArrayList<Groups>(); 
       for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) { 
        System.out.println(dataSnapshot1.getValue()); 

        Groups value = dataSnapshot1.getValue(Groups.class); 
        Groups fire = new Groups(); 
        String name = value.getGroupName(); 
        String bannerImage = value.getGroupBannerImage(); 
        String iconImage = value.getGroupIcon(); 
        String description = value.getGroupDescription(); 
        String dateCreated = value.getGroupCreatedDate(); 
        Boolean NSFW = value.getGroupNSFW(); 
        Boolean openPublic = value.getGroupOpenPublic(); 
        Integer followers = value.getGroupNoOfFollowers(); 
        Integer posts = value.getGroupNoOfPosts(); 

        fire.setGroupName(name); 
        //fire.setGroupBannerImage(bannerImage); 
        //fire.setGroupIcon(iconImage); 
        //fire.setGroupDescription(description); 
        //fire.setGroupCreatedDate(dateCreated); 
        //fire.setGroupNSFW(NSFW); 
        //fire.setGroupOpenPublic(openPublic); 
        //fire.setGroupNoOfFollowers(followers); 
        //fire.setGroupNoOfPosts(posts); 

        Toast.makeText(Hot.this, fire.group_name , Toast.LENGTH_LONG).show(); 
        list.add(value); 

       } 
       send(); 
      } 


      @Override 
      public void onCancelled(DatabaseError error) { 
       // Failed to read value 
       System.out.println("Failed to read value." + error.toException()); 
      } 


     }); 
public void send(){ 
     RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(list, Hot.this); 
     //RecyclerView.LayoutManager recyce = new GridLayoutManager(Hot.this, 2); 
     RecyclerView.LayoutManager recyce = new LinearLayoutManager(Hot.this); 
     //recyclerViews.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true)); 
     recyclerViews.setLayoutManager(recyce); 
     recyclerViews.setItemAnimator(new DefaultItemAnimator()); 
     recyclerViews.setAdapter(recyclerViewAdapter); 
} 
0

試してみて、Recycleview

recyclerView.setHasFixedSize(true); 

おかげで、この行を追加します..!

関連する問題