2016-05-13 5 views
1

これは私のコードです。ここでは最初に0個の要素を持つoriginalProductListという値にアダプタを設定しています。後、私は1595(データ数)このadapter.notifyDataSetChanged()を呼び出すadapter.notifyDataSetChanged()がアンドロイドで動作していません

private List<ParentListItem> originalProductList = new ArrayList<>(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    recyclerView = (RecyclerView) findViewById(R.id.crime_recycler_view); 
    recyclerView.setLayoutManager(new LinearLayoutManager(this)); 
    adapter = new MyAdapter(this, originalProductList); 
    adapter.onRestoreInstanceState(savedInstanceState); 
    recyclerView.setAdapter(adapter); 

    getProducts(); 

} 

private void getProducts() { 
    if (Utility.getParentListItems().size() == 0) { 
     final ProgressDialog loading = new ProgressDialog(ActivityProductList.this, R.style.MyTheme); 
     loading.setCancelable(true); 
     loading.show(); 

     Retrofit retrofit = new Retrofit.Builder() 
       .baseUrl(Constants.BASERESTURL) 
       .addConverterFactory(GsonConverterFactory.create()) 
       .build(); 

     RestInterface service = retrofit.create(RestInterface.class); 
     Call<List<Product>> call = service.getProducts(); 

     call.enqueue(new Callback<List<Product>>() { 

      @Override 
      public void onResponse(Response<List<Product>> response, Retrofit retrofit) { 
       List<Product> products = response.body(); 
       loading.dismiss(); 


       //logic to parse data 
       Utility.setParentListItems(parentListItems); 
       originalProductList.clear(); 
       originalProductList.addAll(Utility.getParentListItems()); 
       Utility.displayToast("in fetch: " + originalProductList.size()); // this prints 1595 
       adapter.notifyDataSetChanged(); 


      } 
      @Override 
      public void onFailure(Throwable t) { 
       //Utility.displaySnackBar(coordinatorLayout, "INTERNET CONNECTION LOST"); 
       Utility.displayToast("some error"); 
       loading.dismiss(); 
      } 
     }); 
    } 
} 

を印刷し、このフィールドを更新しています。 UIを更新するのではなく、エラーもありません。

ロジックに問題がありますか?これを修正するには?

+0

は、問題は、あなただけではなく、アダプター内のリストを変更している、です。必要なことは、アダプタ内で新しいリストをパラメータ(setOriginalProductList)として渡し、このメソッド内でnotifyDatasetChaged()を呼び出すことです。 – Leonardo

+0

応答はバックグラウンドスレッド上にある可能性があります。あなたの 'notifyDataSetChanged()'を 'runOnUiThread'で囲んでみてください。 – dharms

答えて

0

したがって、上記の私のコメントをclearifyし、あなたがこのような何か行う必要があります。リストを取得した後、

public class DebitAdapter extends RecyclerView.Adapter<DebitAdapter.DebitViewHolder> { 

    private List<Debit> debits; 

    public void setDebits(List<Debit> debits) { 
     this.debits = debits; 
     notifyDataSetChanged(); 
    } 

    @Override 
    public DebitViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     return null; 
    } 

    @Override 
    public void onBindViewHolder(DebitViewHolder holder, int position) { 

    } 

    @Override 
    public int getItemCount() { 
     return debits != null ? debits.size() : 0; 
    } 

    static class DebitViewHolder extends RecyclerView.ViewHolder { 

     public DebitViewHolder(View itemView) { 
      super(itemView); 
     } 
    } 

} 

そして、あなたのコールバックの内側に、このような何か:もちろん

List<Product> products = response.body(); 
       loading.dismiss(); 


       //logic to parse data 
       Utility.setParentListItems(parentListItems); 
       originalProductList.clear(); 
       originalProductList.addAll(Utility.getParentListItems()); 
       Utility.displayToast("in fetch: " + originalProductList.size()); // this prints 1595 
       adapter.setOriginalProductList(originalProductList); 

上記アダプタは、アダプタを作成する方法の一例に過ぎず、完全に動作させる必要があります。

おかげ

1
@Override 
public void onResponse(Response<List<Product>> response, Retrofit retrofit) { 
    List<Product> products = response.body(); 
    loading.dismiss(); 

    //logic to parse data 
    Utility.setParentListItems(parentListItems); 
    originalProductList.clear(); 
    originalProductList.addAll(Utility.getParentListItems()); 
    madapter.addData(originalProductList); 
} 


public class Adapter { 
    ... 
    private List<Product> product; 

    public void addData(List<Product> product) { 
     this.product= product; 
     notifyDataSetChanged(); 
    } 
} 
関連する問題