Dr. KirKhan Dr. KirKhan - 25 days ago 6
Android Question

How to initialize view in Adapter in Android

WHY NOT ANY PEOPLE HELP ME??

In my application I want to use

RecyclerView Adapter
and I should Call API into this adapter and in onResponse update text of
TextView
.

My adapter code:

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

private Context context;
private List<CommentData> model;
private int movieID;
private String token;
//Reply dialog views
private MovieResponse movieResponse;
private ImageView dialogReply_headerClose, row_commentProfileImage;
private EditText dialog_watchlistEdtTxt;
private LinearLayout dialog_watchlistSubmitLay;
private TextView row_commentNameTxt, row_commentCommentTxt, row_commentLikeTxt, row_commentDateTxt;
private RecyclerView dialogReply_recyclerView;
private ProgressBar dialog_watchlistSubmitProgress, dialogReply_recyclerViewProgress;
private RelativeLayout row_commentLikeLay, row_commentReplayLay;
private Dialog replyDialog;
private CommentsReplyAdapter commentsReplyAdapter;
private List<Datum> replyModel = new ArrayList<>();

private CommentsListAdapter.ViewHolder holder;

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

@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 = holder;

holder.row_commentNameTxt.setText(Html.fromHtml(model.get(position).getOwner().getName()));
holder.row_commentCommentTxt.setText(Html.fromHtml(model.get(position).getText()));
//holder.row_newsBig_imageProgress.show();
Glide.with(context)
.load(model.get(position).getOwner().getImageUrl())
.asBitmap()
.placeholder(R.drawable.default_image)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.override(500, 500)
.into(new BitmapImageViewTarget(holder.row_commentProfileImage) {
@Override
protected void setResource(Bitmap resource) {
if (context == null) return;

RoundedBitmapDrawable circularBitmapDrawable =
RoundedBitmapDrawableFactory.create(context.getResources(), resource);
circularBitmapDrawable.setCircular(true);
holder.row_commentProfileImage.setImageDrawable(circularBitmapDrawable);
}
});
holder.row_commentLikeTxt.setText(model.get(position).getLikeCount() + " " + context.getResources().getString(R.string.like));
holder.row_commentReplayTxt.setText(model.get(position).getRepliesCount() + " " + context.getResources().getString(R.string.reply));
holder.row_commentDateTxt.setText(model.get(position).getSubmitDate() + " " + model.get(position).getSubmitTime());
//Reply click
holder.row_commentReplayLay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

}
});
//Like click
holder.row_commentLikeLay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

}
});
}

private void getMiniData() {
MovieSendData sendData = new MovieSendData();
sendData.setMovieId(movieID);

InterfaceApi api = ApiClient.getClient().create(InterfaceApi.class);
Call<MovieResponse> call = api.getMovieDetail(token, sendData);

call.enqueue(new Callback<MovieResponse>() {
@Override
public void onResponse(Call<MovieResponse> call, Response<MovieResponse> response) {
if (response.body().getData() != null) {
movieResponse = response.body();
row_commentReplayTxt.setText(movieResponse.getData().getUserReviewEntity().getRepliesCount()
+ " " + context.getResources().getString(R.string.reply));
}
}

@Override
public void onFailure(Call<MovieResponse> call, Throwable t) {
}
});
}

@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 ImageView row_commentProfileImage;
private TextView row_commentNameTxt, row_commentCommentTxt, row_commentLikeTxt, row_commentReplayTxt, row_commentDateTxt;
private RelativeLayout row_commentReplayLay, row_commentLikeLay;

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

row_commentProfileImage = (ImageView) 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);
row_commentReplayLay = (RelativeLayout) itemView.findViewById(R.id.row_commentReplayLay);
row_commentLikeLay = (RelativeLayout) itemView.findViewById(R.id.row_commentLikeLay);
}
}
}


But row_commentReplayTxt is not initialized in getMiniData.

How can I fix it? please help me

Answer Source

Try this code :

Initialize global

MovieCommentListAdapter.ViewHolder viewHolder;

In onBindViewHolder

viewHolder = holder;

Then, in your custom method (getMiniData)

viewHolder.row_commentReplayTxt.setText(movieResponse.getData().getUserReviewEntity().getRepliesCount()
                        + " " + context.getResources().getString(R.string.reply));

I hope help you