回收站视图无显示

时间:2019-12-19 18:36:34

标签: java android android-recyclerview

活动

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

这是我的适配器 MyAdapter

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的新手,我想从火力基地读取数据,但是当我使用回收站视图时,我的活动没有显示任何活动。

我无法理解为什么我的recyclerview没有在活动中显示任何内容。请帮助我。

2 个答案:

答案 0 :(得分:0)

在方法getItemCount()我的适配器类中,它显示为return 0;

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

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

答案 1 :(得分:0)

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

/v1.0/users?$select=id,userPrincipalName,assignedLicenses

MyAdapter应该在适配器中返回许多项目以进行渲染。试试这个:

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

当然,getItemCount()应该是@Override public int getItemCount() { return this.postlist.size(); } ,以便呈现任何项目