我似乎无法在代码中找到显示卡片的错误。
我使用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>
有什么东西我缺乏或做错了,因为我的回收者视图根本没有显示任何内容。
答案 0 :(得分:0)
在网上搜索后,我发现不是将我的RecyclerViews放在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);
}
答案 1 :(得分:0)
尝试添加此行以进行回收查看
recyclerView.setHasFixedSize(true);
谢谢.. !!