2017-07-04 25 views
-1

我想一些數據顯示爲recyclerview設置數據,爲此我使用下面顯示的代碼。如何recyclerView與改造Android中

當我運行應用程序,它不顯示我的任何數據到recyclerviewListData.size()是,但在PostMan

我下面寫的一組代碼放入recyclerView

InterfaceApi api = ApiClient.getClient().create(InterfaceApi.class); 
Call<CommentResponse> call = api.getComments(sendData); 

call.enqueue(new Callback<CommentResponse>() { 
    @Override 
    public void onResponse(Call<CommentResponse> call, Response<CommentResponse> response) { 
     if (response.body().getData() != null) { 
      commentModel.clear(); 
      commentModel.addAll(response.body().getData()); 
      commentsListAdapter.notifyDataSetChanged(); 
      content_newsCommentsRecyclerView.setAdapter(commentsListAdapter); 
     } 
    } 

    @Override 
    public void onFailure(Call<CommentResponse> call, Throwable t) { 

    } 
}); 

我的適配器代碼:

public class CommentsListAdapter extends RecyclerView.Adapter<CommentsListAdapter.ViewHolder> { 

    private Context context; 
    private List<CommentData> model; 

    public CommentsListAdapter(Context context, List<CommentData> model) { 
     this.context = context; 
     this.model = model; 
    } 

    @Override 
    public CommentsListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_comment, parent, false); 

     return new CommentsListAdapter.ViewHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(final CommentsListAdapter.ViewHolder holder, final int position) { 

     holder.row_commentNameTxt.setText(Html.fromHtml(model.get(position).getOwner().getName())); 
     holder.row_commentCommentTxt.setText(Html.fromHtml(model.get(position).getText())); 
     Glide.with(context) 
       .load(model.get(position).getOwner().getImageUrl()) 
       .placeholder(R.drawable.default_image) 
       .diskCacheStrategy(DiskCacheStrategy.ALL) 
       .listener(new RequestListener<String, GlideDrawable>() { 
        @Override 
        public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) { 
         return false; 
        } 

        @Override 
        public boolean onResourceReady(GlideDrawable resource, String model, 
                Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) { 
         return false; 
        } 
       }) 
       .into(holder.row_commentProfileImage); 
     holder.row_commentLikeTxt.setText(model.get(position).getLikeCount() + ""); 
     holder.row_commentReplayTxt.setText(model.get(position).getRepliesCount() + ""); 
     holder.row_commentDateTxt.setText(model.get(position).getSubmitDate() + " " + model.get(position).getSubmitTime()); 
    } 

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

    public void addNewItem(List<CommentData> newContent) { 
     int start = this.model.size(); 
     int end = newContent.size(); 
     model.addAll(newContent); 
     notifyDataSetChanged(); 
    } 

    public void clear() { 
     model.clear(); 
     notifyDataSetChanged(); 
    } 

    public class ViewHolder extends RecyclerView.ViewHolder { 

     private CircleImageView row_commentProfileImage; 
     private TextView row_commentNameTxt, row_commentCommentTxt, row_commentLikeTxt, row_commentReplayTxt, row_commentDateTxt; 

     public ViewHolder(View itemView) { 
      super(itemView); 

      row_commentProfileImage = (CircleImageView) itemView.findViewById(R.id.row_commentProfileImage); 
      row_commentNameTxt = (TextView) itemView.findViewById(R.id.row_commentNameTxt); 
      row_commentCommentTxt = (TextView) itemView.findViewById(R.id.row_commentCommentTxt); 
      row_commentLikeTxt = (TextView) itemView.findViewById(R.id.row_commentLikeTxt); 
      row_commentReplayTxt = (TextView) itemView.findViewById(R.id.row_commentReplayTxt); 
      row_commentDateTxt = (TextView) itemView.findViewById(R.id.row_commentDateTxt); 

     } 
    } 
} 

如何將數據顯示到recyclerView

+0

您的評論數據模型類? –

+0

可能是其json解析問題 –

+0

@GaganDeep,請參閱我的更新文章我的朋友。請參閱並幫助我 – Jongle

回答

0

適配器類

 //This will add all the items in the adapter's list 
     public void addAllItems(List<CommentData> items) { 
       model.addAll(items); 
       notifyDataSetChanged(); 
      } 

//In Adapter's Constructor do the following changes 
public CommentsListAdapter(Context context, List<CommentData> model) { 
     this.context = context; 
     this.model = new ArrayList<>; 
    } 

內創建一個方法,當你取你的迴應,你可以通過

//Inside your onCreate add the below code 

     mAdapter = new CommentsListAdapter (this); 
     content_newsCommentsRecyclerView.setLayoutManager(new LinearLayoutManager(this)); 
     content_newsCommentsRecyclerView.setAdapter(mAdapter); 

//Call this inside your success of onResponse 
commentsListAdapter.addAllItems(commentModel); 

稱之爲這將更新recyclerView的內容和通知所做的更改,如果你有任何問題,請試試這個,讓我知道。

+0

不再向我顯示的數據爲recyclerView :( – Jongle

+0

commentModel是你的ArrayList右 –

+0

是的,我初始化這個:私人名單 commentModel =新的ArrayList <>(); – Jongle

0

下面替換您onResponse方法:

@Override 
    public void onResponse (Call <CommentResponse> call, Response <CommentResponse> response) 
    { 
     if (response.body().getData() != null) { 
      commentModel.clear(); 
      commentModel.addAll(response.body().getData()); 
      content_newsCommentsRecyclerView.setAdapter(new CommentsListAdapter(getApplicationContext(), commentModel)); 
     } 
    } 

而且還onCreate intializing Recyclerview

content_newsCommentsRecyclerView.setLayoutManager的ID後面添加以下行(新LinearLayoutManager(getApplicationContext())) ;

0

API調用之前初始化適配

mLayoutManager = new LinearLayoutManager(getActivity()); 
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); 
mRVCommentList.setLayoutManager(mLayoutManager); 
CommentsListAdapter commentsListAdapter= new CommentsListAdapter(this) 
mRVCommentList.setAdapter(commentsListAdapter) 

然後調用API

public void onResponse(Call<CommentResponse> call, Response<CommentResponse> response) { 
     if (response.body().getData() != null) { 
      List<CommentData> list= response.body().getData().getCommentData(); 
      commentsListAdapter.setData(list); 
     } 
    } 
在適配器類別

小的變化10

+0

不初始化列表中的getCommentData() list = response.body()。getData()。getCommentData(); – Jongle

+0

請幫助我的朋友 – Jongle

+0

@Jongle你可以確保response.body()包含所需的數據 – Sibin