2017-04-18 116 views
0

我似乎無法找到我的代碼中顯示卡的錯誤。 我正在使用Firebase將我的數據繪製成卡片,到目前爲止,我只是首先嚐試顯示group_nameCardView不顯示

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();並且正如你在onCreate中所做的那樣,你必須檢查adapter是否爲null,然後才能notifyDatsetChanged –

+0

當我recyclerViews.getAdapter()。getItemCount()它返回0,但是當我添加到列表中時,list.size ()返回1.這是否意味着我的RecyclerViewAdapter的邏輯錯誤? –

+0

它的工作方式與adapter.getItemCount()一樣,返回列表大小爲no的項,您已在yourAdapter類中覆蓋該方法。 –

回答

0

在網上搜索後,我發現不是將我的RecyclerViews放置在onCreate方法中,而是將我所有的RecyclerViews放入一個方法中,一旦我的列表完全添加完畢,就會調用它。

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

謝謝..!