2016-09-01 5 views
0

私はapiからいくつかの画像を取得していますが、glideライブラリを使用してImageViewに画像を読み込んでいますが、これが起こっています。画像はロードされ、おそらくオブジェクトの配列に値として格納されますが、その配列はrecyclerviewのアダプタには付けられません。私は多くの組み合わせを試していました。コードのブロックの後ろにrecyclerviewの初期化を置いて、配列にアイテムを追加した後、メソッドnotifyDataSetChanged()を使用してアダプタを更新しましたが、結果が得られませんでした。また、私は、すべての項目を追加した後、recyclerviewとアダプタの初期化を試みましたが、まだ効果はありません。ここに私のコードです:RecyclerViewにオブジェクトの配列リストが設定されていません

private void configureRecyclerView() { 
    userRecyler = (RecyclerView) findViewById(R.id.list); 
    userRecyler.setHasFixedSize(true); 

    LinearLayoutManager horizontalLayoutManager = 
      new LinearLayoutManager(HomeActivity.this, LinearLayoutManager.HORIZONTAL, false); 
    userRecyler.setLayoutManager(horizontalLayoutManager); 
    userRecyler.setItemAnimator(new DefaultItemAnimator()); 

    mAdapter = new UserListAdapter(HomeActivity.this, mUsers); 
    userRecyler.setAdapter(mAdapter); 
} 

この質問への追加アダプタクラス(更新)今、すべてのrecyclerviewリストが接続されている

public class UserListAdapter extends RecyclerView.Adapter<UserListAdapter.UserViewHolder> { 

private List<User> mUsers; 
private Context mContext; 

public UserListAdapter(Context context, List<User> users) { 
    mContext = context; 
    mUsers = users; 
} 


@Override 
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View view = LayoutInflater.from(mContext).inflate(R.layout.user_lst_item, parent, false); 

    return new UserViewHolder(view); 
} 

@Override 
public void onBindViewHolder(UserViewHolder holder, int position) { 
    User user = mUsers.get(position); 

    Glide.with(mContext) 
      .load("http://graph.facebook.com/" + user.getUserId() + "/picture?width=150&height=150") 
      .fitCenter() 
      .placeholder(R.drawable.ic_ikonica_t) 
      .diskCacheStrategy(DiskCacheStrategy.SOURCE) 
      .into(holder.userProfilePic); 
} 

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

public class UserViewHolder extends RecyclerView.ViewHolder { 
    CircularImageView userProfilePic; 

    public UserViewHolder(View itemView) { 
     super(itemView); 
     userProfilePic = (CircularImageView) itemView.findViewById(R.id.iv_user_image); 
    } 
} 
} 

OnActionListener<List<Like>> onLikeListener = new OnActionListener<List<Like>>() { 
    @Override 
    public void onComplete(List<Like> likes) { 
     mUsers.clear(); 

     for (Like like : likes) { 
      User user = new User(); 
      user.setUserId(like.getUser().getId()); 
      mUsers.add(user); 
     } 

     configureRecyclerView(); 
    } 
}; 

そして方法configureRecyclerView()内部

1つのビューページャのフラグメント、そしてこれは私がスワイプして同じフラグメントに戻ったときに起こっていることです。画像はキャッシュメモリからロードされています。画像がロードされ、値が配列リストに格納されますが、明らかにrecyclerviewには問題があります。誰もこれで私を助けることができますか?

答えて

0

警告:私はこのサイトに参加したばかりですが、私はここで答えを知っていると思います。助けてくれたら教えてください。

編集:画像を取得する最初のメソッドの最後にYourAdapter.notifyDataSetChanged()を使用することもできます。

アダプター内の画像をBindViewHolderメソッドで設定していますか?私は最近、recyclerviewsのためのいくつかのアダプターをセットアップし、それを行うための最良の場所だったことがわかりました。あなたは配列ファイルを渡しているようですが、リサイクラビューの中に設定するだけです。

私のコンストラクタでは、jsonファイルを渡してアダプタに保存し、onBindViewHolderで使用します。

public Adapter_Tastings(JSONObject _tastingsJSON, Context context){ 
    mContext = context; 
    try{ 
     tastingsKey = _tastingsJSON.getJSONArray("key"); 
     tastingsJSON = _tastingsJSON.getJSONObject("tastings"); 
    } catch(JSONException e){ 
     e.printStackTrace(); 
    } 
} 

次に、このファイルの値を使用して次のように設定します。アダプタ内で宣言されているViewHolderクラスは、recyclerviewアイテムレイアウト内のViewsへの実際の参照を保持します。

@Override 
public void onBindViewHolder(Adapter_Tastings.ViewHolder viewHolder, int position){ 
    TextView mTastingDate = viewHolder.tastingDate; 
    TextView mTastingTime = viewHolder.tastingTime; 
    TextView mTastingLocation = viewHolder.tastingLocation; 
    TextView mTastingPhone = viewHolder.tastingPhone; 

    try { 
     String key = tastingsKey.getString(position); 

     mTastingDate.setText(tastingsJSON.getJSONObject(key).getString("date")); 
     mTastingLocation.setText(tastingsJSON.getJSONObject(key).getString("location")); 
     mTastingTime.setText(tastingsJSON.getJSONObject(key).getString("time")); 
     mTastingPhone.setText(tastingsJSON.getJSONObject(key).getString("phone")); 
    } catch(JSONException e){ 
     e.printStackTrace(); 
    } 
} 
+0

私は自分のアダプターで質問を更新します。 –

関連する問題