2016-06-10 143 views
0

在我的適配器和檢索服務器的數據我可以重新加載recyclerview,在我的recyclerview項目我有簡單的imageView和檢索服務器和圖像狀態數據後,我必須改變圖像,但我的適配器無法在重新加載數據和notifyDataSetChanged之後更新映像。安卓更新imageview後適配器notifyDataSetChanged

我可以從服務器通過syncByServer我UPADTE數據庫之後方法,通過這個代碼檢索數據:

ContactLists exists_row = realm.where(ContactLists.class).equalTo("mobile_number", mobileNumber).findFirst(); 
realm.beginTransaction(); 
if (exists_row != null) { 
    exists_row.setIsRegistered("1"); 
} else { 
    exists_row.setIsRegistered("0"); 
} 
realm.commitTransaction(); 

setIsRegistered必須改變圖像狀態,如果setIsRegistered是1,我必須從Drawable設置setImageDrawable

MainActivity:

realm = Realm.getDefaultInstance(); 
recyclerView = (RecyclerView) findViewById(R.id.rcontactLists); 
recyclerView.setLayoutManager(new LinearLayoutManager(this)); 
adapter = new ContactsAdapter(results, getApplication()); 
recyclerView.setAdapter(adapter); 
long contact_count = realm.where(ContactLists.class).count(); 
if (contact_count > 0) { 
    reloadFromDatabase(); 
} else { 
    long count = Utils.getLocalContactsList(getContentResolver(), realm); 
    if (count > 0) { 
     reloadFromDatabase(); 
    } 
    syncByServer(); 
} 

/*----------------------------------------------------------*/ 

private void reloadFromDatabase() { 
    results = realm.where(ContactLists.class).findAll(); 
    adapter.setData(results); 
    adapter.notifyDataSetChanged(); 
} 

private void syncByServer() { 
    long count = Utils.getLocalContactsList(getContentResolver(), realm); 
    if (count > 0) { 
     showProgressDialog(); 
     results = realm.where(ContactLists.class).findAll(); 
     ArrayList wordList = new ArrayList<>(); 
     if (results.size() > 0) { 
      for (ContactLists c : results) { 
       HashMap<String, String> map = new HashMap<String, String>(); 
       map.put("contactName", c.getContact_name()); 
       map.put("mobileNumber", c.getMobile_number()); 
       wordList.add(map); 
      } 
     } 

     String fetch_server_url = SampleApplication.getHostAddress() + "clientSyncContatcLists"; 

     HttpsTrustManager.allowAllSSL(); 
     JsonArrayRequest req = new JsonArrayRequest(fetch_server_url, new JSONArray(wordList), 
       new Response.Listener<JSONArray>() { 
        @Override 
        public void onResponse(JSONArray response) { 
         for (int i = 0; i < response.length(); i++) { 
          try { 
           JSONObject jsonObject = response.getJSONObject(i); 
           String  mobileNumber = jsonObject.getString("mobileNumber"); 

           ContactLists exists_row = realm.where(ContactLists.class).equalTo("mobile_number", mobileNumber).findFirst(); 

           realm.beginTransaction(); 
           if (exists_row != null) { 
            exists_row.setIsRegistered("1"); 
           } else { 
            exists_row.setIsRegistered("0"); 
           } 
           realm.commitTransaction(); 

          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 
         } 

         results = realm.where(ContactLists.class).findAll(); 
         adapter.setData(results); 
         adapter.notifyDataSetChanged(); 
         progressDialog.dismiss(); 
        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       //Log.e 
      } 
     }); 
     EpayApplication.getInstance().addToRequestQueue(req); 
    } else { 
     Utils.toast(Utils.getString(R.string.there_is_no_contacts), getBaseContext()); 
    } 
} 

我的適配器

public class ContactsAdapter extends RecyclerView.Adapter<CustomContactsViewHolder> { 

    List<ContactLists> list = Collections.emptyList(); 
    Context context; 

    public ContactsAdapter(List<ContactLists> list, Context context) { 
     this.list = list; 
     this.context = context; 
    } 

    @Override 
    public CustomContactsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View      v  = LayoutInflater.from(parent.getContext()).inflate(R.layout.contact_item, parent, false); 
     CustomContactsViewHolder holder = new CustomContactsViewHolder(v); 
     return holder; 
    } 

    @Override 
    public void onBindViewHolder(CustomContactsViewHolder holder, int position) { 
     holder.title.setText(list.get(position).getContact_name()); 
     holder.description.setText(list.get(position).getMobile_number()); 
     if (list.get(position).getIsRegistered().equals("1")) 
      holder.img_logo.setImageDrawable(context.getResources().getDrawable(R.drawable.epay_logo)); 
     else 
      holder.img_logo.setVisibility(View.INVISIBLE); 
    } 

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

    @Override 
    public void onAttachedToRecyclerView(RecyclerView recyclerView) { 
     super.onAttachedToRecyclerView(recyclerView); 
    } 

    public void setData(List<ContactLists> list) { 
     this.list = list; 
    } 
} 

回答

0

您可能正在運行到NPE。發佈的代碼片段保證exists_rownull在else分支中。

if (exists_row != null) { 
    exists_row.setIsRegistered("1"); 
} else { 
    exists_row.setIsRegistered("0"); 
} 

還有一個問題,爲什麼IDE沒有提醒你呢?

exists_row從來沒有null。我認爲這可能是這種情況,因爲你沒有寫這個應用程序崩潰。調試該行以查看究竟發生了什麼。

+0

with'exists_row'我可以檢查數據庫的存在行,如果它不是null,那麼'mobileNumber'在我的數據庫中,我必須將'setIsRegistered'字段更改爲'1',這不是我的問題,工作正常,但改變後數據庫我得到數據庫的所有領域,我希望改變'imageView'內容的這種方法:'onBindViewHolder'在適配器 –

+0

再次設置適配器'recyclerView'解決了我的問題 –

相關問題