2016-09-01 117 views
0

我有,我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(); 
    } 
} 

如果我將項目添加到數據庫中,項目將顯示 - 這是好的,但如果數據庫是不是空的,它顯示了什麼都沒有,我如何顯示在我的適配器中的項目?

+0

這個代碼不正確,修復第一... – BruceStackOverFlow

+0

@BruceStackOverFlow:不要簡單地說,不正確或一些東西 說,一些使用全這樣他就可以解決問題。 ... 如果不正確,至少識別並告訴他。 –

+0

@蘭姆好。我打算調用一個不存在的函數 setRecyclerView。並且很難理解什麼是錯誤的... – BruceStackOverFlow

回答

0

您應該在獲得數據後更新適配器。現在你有一個異步事件,你在完成之前設置recycleview。

+0

方法setRecyclerView()是好的,問題是當我從數據庫中獲取數據,所以:在數據庫中有一些數據。 – Stepan

+0

你能分享一下setRecyclerView()方法嗎? – breakline

+0

是的,我編輯我的問題 – Stepan

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(); 
    } 
} 
+0

感謝,但它沒有幫助我,但不幸的是它是相同的 - 數據不可見 – Stepan

相關問題