簡體   English   中英

CardView不顯示

[英]CardView not showing

我似乎無法在顯示卡的代碼中找到錯誤。 我正在使用Firebase將數據提取到卡中,到目前為止,我只是嘗試首先顯示group_name

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>

我的Firebase數據

我的recyclerview根本不顯示任何內容嗎?

在網上搜索后,我發現不要將我的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);
}

嘗試添加此行用於Recycleview

recyclerView.setHasFixedSize(true);

謝謝..!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM