neustart47 neustart47 - 3 months ago 13
Android Question

Changing text in TextView inside CardView from itself

Imagine a CardView with a button and text field. When I press on a button, text should change in the textfield. Code for my recyclerView is given below, where I am setting the text and a listener for a button:

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

final CategoryDetailedModel model = categoriesDetailedModels.get(position);
holder.description.setText(model.getQuestion());

holder.btnPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.description.setText("some text");
}
});
}


The main problem is with the string:

holder.description.setText("some text");


It cannot be accessed from an inner class.
Is anyone have any idea how to change text in card view from itself.

Update 1

public class CategoriesDetailedViewHolder extends RecyclerView.ViewHolder{
CardView cv;
TextView description;
TextView positiveCounter;
TextView negativeCounter;
TextView btnPlus;
View negativeCounterWrapper;
View positiveCounterWrapper;
CategoriesDetailedViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);
description = (TextView)itemView.findViewById(R.id.main_text);
positiveCounter = (TextView)itemView.findViewById(R.id.tv_positive_count_in_circle);
negativeCounter = (TextView)itemView.findViewById(R.id.tv_negative_count_in_circle);
btnPlus = (TextView)itemView.findViewById(R.id.plus_button);

positiveCounterWrapper = itemView.findViewById(R.id.positive_count_wrapper);
negativeCounterWrapper = itemView.findViewById(R.id.negative_count_wrapper);
cv.setPreventCornerOverlap(false);
}
}

@Override
public CategoriesDetailedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.categories_detailed_list_row_view, parent, false);
CategoriesDetailedViewHolder pvh = new CategoriesDetailedViewHolder(v);
context = parent.getContext();
return pvh;
}

Answer

Instead of,

TextView description;

Make it,

public TextView description;

EDIT: Change the following,

@Override
    public void onBindViewHolder(CategoriesDetailedViewHolder holder, int position) { ... }

to,

@Override 
    public void onBindViewHolder( final CategoriesDetailedViewHolder holder, int position) { .... }
Comments