我有,我POST請求到服務器,如果數據庫是空的,我添加項目到數據庫中,如果在數據庫中的一些項目,只是使它們在適配器的方法。沒有顯示物品recyclerView
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();
}
}
如果我將項目添加到數據庫中,項目將顯示 - 這是好的,但如果數據庫是不是空的,它顯示了什麼都沒有,我如何顯示在我的適配器中的項目?
這個代碼不正確,修復第一... – BruceStackOverFlow
@BruceStackOverFlow:不要簡單地說,不正確或一些東西 說,一些使用全這樣他就可以解決問題。 ... 如果不正確,至少識別並告訴他。 –
@蘭姆好。我打算調用一個不存在的函數 setRecyclerView。並且很難理解什麼是錯誤的... – BruceStackOverFlow