在recyclerView中没有显示项目

时间:2016-09-01 12:10:51

标签: android android-recyclerview fragment retrofit

我有一种方法可以将请求发布到服务器,如果数据库为空,我添加项到数据库,如果数据库中有一些项目,只需渲染它们在适配器。

 private void getContacts() {
        database = SugarRecord.listAll(ContactORM.class);

        if(database.isEmpty()) {
            showProgressDialog();
            FactoryAPI.getContacts().getContacts(user.getToken()).enqueue(new Callback<ContactsResponse>() {
                @Override
                public void onResponse(Call<ContactsResponse> call, Response<ContactsResponse> response) {
                    if (response.isSuccessful()) {
                        contactList = response.body().getContactsList();
                        if (database.isEmpty()) {
                            for (int i = 0; i < contactList.size(); i++) {
                                ContactORM contact = new ContactORM();
                                contact.setId(contactList.get(i).getId());
                                contact.setName(contactList.get(i).getName());
                                contact.setLastname(contactList.get(i).getLastname());
                                contact.setEmail(contactList.get(i).getEmail());
                                contact.setPhoto(contactList.get(i).getPhoto());
                                contact.save();
                                database.add(contact);
                            }
                        }
                        progressDialog.dismiss();
                        setRecyclerView();
                    }
                }

                @Override
                public void onFailure(Call<ContactsResponse> call, Throwable t) {
                    Toast.makeText(getContext(), "failure", Toast.LENGTH_SHORT).show();
                }
            });
        }
        //set recycler view
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new ContactsAdapter(getContext(), database);
        recyclerView.setAdapter(mAdapter);

    }

setRecyclerView方法:

database = SugarRecord.listAll(ContactORM.class); 
recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); 
mAdapter = new ContactsAdapter(getContext(), database); recyclerView.setAdapter(mAdapter);

适配器:

class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.ViewHolder> {

    List<ContactORM> database;
    Context mContext;

    public ContactsAdapter(Context context, List<ContactORM> contacts) {
        this.mContext = context;
        this.database = contacts;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int type) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item_contact, parent, false);
        return new ViewHolder(view);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.avatar) CircleImageView avatar;
        @BindView(R.id.name) TextView name;

        public ViewHolder(View rowView) {
            super(rowView);
            ButterKnife.bind(this, rowView);
        }
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        ContactORM contact = database.get(position);
        if ((contact.getLastname() != null)) {
            holder.name.setText(contact.getName() + " " + contact.getLastname());
        } else {
            holder.name.setText(contact.getName());
        }
        //if photo is not null, load image
        if (!(contact.getPhoto() == null)) {
            Picasso.with(mContext).load(contact.getPhoto()).resize(100, 100).into(holder.avatar);
        }
    }

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

如果我将项目添加到数据库,则会显示项目 - 它没关系,但是如果数据库不为空,则无法显示如何在我的适配器中显示该项目?

2 个答案:

答案 0 :(得分:0)

您应该在获得数据后更新适配器。现在你有了一个异步事件,并在完成之前设置了recycleview。

答案 1 :(得分:0)

试试这个......

.....
// initialize database here
private ContactsAdapter mAdapter;
private RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Initialize recycler view
    mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setAdapter(mAdapter);

    // database is must not be null here; if null initialize it.
    mAdapter = new ContactsAdapter(getContext(), database);
    getContacts();
}

private void getContacts() {
    database = SugarRecord.listAll(ContactORM.class);

    if(database.isEmpty()) {
        showProgressDialog();
        FactoryAPI.getContacts().getContacts(user.getToken()).enqueue(new Callback<ContactsResponse>() {
            @Override
            public void onResponse(Call<ContactsResponse> call, Response<ContactsResponse> response) {
                if (response.isSuccessful()) {
                    contactList = response.body().getContactsList();
                    if (database.isEmpty()) {
                        for (int i = 0; i < contactList.size(); i++) {
                            ContactORM contact = new ContactORM();
                            contact.setId(contactList.get(i).getId());
                            contact.setName(contactList.get(i).getName());
                            contact.setLastname(contactList.get(i).getLastname());
                            contact.setEmail(contactList.get(i).getEmail());
                            contact.setPhoto(contactList.get(i).getPhoto());
                            contact.save();
                            database.add(contact);
                        }
                    }
                    progressDialog.dismiss();
                    mAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<ContactsResponse> call, Throwable t) {
                Toast.makeText(getContext(), "failure", Toast.LENGTH_SHORT).show();
            }
        });
    }else {
        mAdapter.notifyDataSetChanged();
    }
}