【问题标题】:Recycler View Show Nothing [duplicate]回收站视图不显示任何内容[重复]
【发布时间】:2019-12-19 18:36:34
【问题描述】:

活动

public class Home extends AppCompatActivity {
RecyclerView recyclerView;
FloatingActionButton floatingActionButton1,;
BottomNavigationView bottomNavigationView;
List<Post> list;
MyAdapter adapter;
private DatabaseReference databaseReference;
ProgressDialog progressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    list=new ArrayList<>();
    recyclerView=findViewById(R.id.recycler_View);
    bottomNavigationView=findViewById(R.id.bottom_navigation);
    floatingActionButton1=findViewById(R.id.floatingActionButton);
    progressDialog=new ProgressDialog(this);
    progressDialog.setMessage("Loading.....");
    bottomNavigationView.setOnNavigationItemReselectedListener(new 
BottomNavigationView.OnNavigationItemReselectedListener() {
        @Override
        public void onNavigationItemReselected(@NonNull MenuItem menuItem) {
            switch(menuItem.getItemId())
            {
                case R.id.action_home:
                    Toast.makeText(Home.this, "home", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.action_profile:
                    Toast.makeText(Home.this, "profile", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.action_recent:
                    Toast.makeText(Home.this, "recent", Toast.LENGTH_SHORT).show();
                    break;
            }
        }

    });



    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    adapter = new MyAdapter(Home.this,list);
    recyclerView.setAdapter(adapter);


    //COde for Firebase post Retrieve

    databaseReference= FirebaseDatabase.getInstance().getReference("post");
    progressDialog.show();
    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            list.clear();
                for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                    Post post = postSnapshot.getValue(Post.class);
                    list.add(post);

                }
                adapter.notifyDataSetChanged();
                progressDialog.dismiss();

        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

public void gotoupload(View view) {

    startActivity(new Intent(getApplicationContext(),UploadPost.class));
}  }

这是我的适配器 我的适配器

package com.example.myapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.PostViewHolder> {

Context context;
List<Post> postlist;

public MyAdapter(Context context, List<Post> postlist) {
    this.context = context;
    this.postlist = postlist;
}

@Override
public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view= LayoutInflater.from(context).inflate
            (R.layout.row_item,parent,false);
    PostViewHolder postViewHolder=new PostViewHolder(view);
    return postViewHolder;
}

@Override
public void onBindViewHolder(@NonNull PostViewHolder holder, int position) {
        Post post=postlist.get(position);
        holder.textViewTitle.setText(post.getTextTitle());
        holder.textViewName.setText( post.getTextAuthor());
        holder.textViewdesc.setText(post.getTextDesc());
}

@Override
public int getItemCount() {
    return 0;
}

class PostViewHolder extends RecyclerView.ViewHolder{
    ImageView imageViewprofile, imageViewpost;
    TextView textViewName,textViewTitle,textViewdesc;

    public PostViewHolder(@NonNull View itemView) {
        super(itemView);
        imageViewpost=itemView.findViewById(R.id.Postimage);
        imageViewprofile=itemView.findViewById(R.id.profile_Rec);
        textViewdesc=itemView.findViewById(R.id.description);
        textViewName=itemView.findViewById(R.id.username1);
        textViewTitle=itemView.findViewById(R.id.Rtitle);

    }
}}

这是我的模型课 模型类命名帖子 包 com.example.myapplication;

public class Post {

public String postimageView;
public String proimage;
public String textTitle;
public String textAuthor;
public String textDesc;
public String textUrl;

public Post(){

}
public Post(String postimageView, String proimage, String textTitle, String textAuthor, String 
textDesc, String textUrl) {
    this.postimageView = postimageView;
    this.proimage = proimage;
    this.textTitle = textTitle;
    this.textAuthor = textAuthor;
    this.textDesc = textDesc;
    this.textUrl = textUrl;
}

public String getPostimageView() {
    return postimageView;
}

public void setPostimageView(String postimageView) {
    this.postimageView = postimageView;
}

public String getProimage() {
    return proimage;
}

public void setProimage(String proimage) {
    this.proimage = proimage;
}

public String getTextTitle() {
    return textTitle;
}

public void setTextTitle(String textTitle) {
    this.textTitle = textTitle;
}

public String getTextAuthor() {
    return textAuthor;
}

public void setTextAuthor(String textAuthor) {
    this.textAuthor = textAuthor;
}

public String getTextDesc() {
    return textDesc;
}

public void setTextDesc(String textDesc) {
    this.textDesc = textDesc;
}

public String getTextUrl() {
    return textUrl;
}

public void setTextUrl(String textUrl) {
    this.textUrl = textUrl;
}}

我是 android 新手,我想从 fire base 读取数据,但是当我使用回收站视图时,我的活动没有显示活动中的任何内容。

我不明白为什么我的 recyclerview 在活动中没有显示任何内容。请帮帮我。

【问题讨论】:

    标签: java android android-recyclerview


    【解决方案1】:

    在方法 getItemCount()My Adapter 类中,它显示为 return 0;

    将其更改为列表的大小,例如

    @Override
    public int getItemCount() {
       return postlist.size();
    }
    

    【讨论】:

      【解决方案2】:

      这可能是个问题(MyAdapter 类):

      @Override
      public int getItemCount() {
          return 0;
      }
      

      getItemCount() 应该在您的适配器中返回许多项目以呈现任何项目。试试这个:

      @Override
      public int getItemCount() {
          return this.postlist.size();
      }
      

      当然,this.postlist.size() 应该是 &gt; 0 以便渲染任何项目

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多