I've created CardView inside RecyclerView.
My XML for CardView looks like this:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@ id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</androidx.cardview.widget.CardView>
I've created the Adapter and successfully shown the data to CardView that inside RecyclerView.
But I failed when I added Button in my activity_main.xml to change size of textView. My program looks like this:
textView =findViewById(R.id.textView);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textView.setTextSize(42);
}
});
The error message is:
2022-04-18 13:28:47.264 3144-3144/com.example.myapplication
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 3144
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTextSize(float)' on a null object reference
at com.example.myapplication.MainActivity$1.onClick(MainActivity.java:39)
How to solve those problems?
CodePudding user response:
To Update text of recycleview item
get position of that item in Recycleview
I am assuming you have a list of items that you passed to adapter , get that item using position you get from step 1 e.g list.get(postion)
now update this item field that shows text on item
user adapter.notifyItemChanged(postion , updatedItem
Pseudo Kotlin code-->
fun updateTextView(position:Int){
val item = list[position]
item.text = "new_text"
adapter.notifyItemChanged(postion , item)
}
CodePudding user response:
You cannot access TextView
directly in RecyclerView
this way. You should get a ViewHolder
via adapter and change it there.
val item = adapter.get(1) // Or another method to get an item from a collection
See How can I access the viewholder of an item in a recycler view? to access ViewHolder
directly. Or change TextView
in a listener bound in onBindViewHolder
.
CodePudding user response:
Don't Initialize your textView in MainActivity
Do it in your adapter
Like below example :
<<<<<<<<<>>>>>>>
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private Context mContext;
private List<ModelRecycler> mData;
public MyAdapter (Context mContext, List<ModelRecycler> mData) {
this.mContext = mContext;
this.mData = mData;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v;
LayoutInflater inflater = LayoutInflater.from(mContext);
v = inflater.inflate(R.layout.list_item, parent, false);
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.textView.setTextSize(42);
}
@Override
public int getItemCount() {
return mData.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView textView;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.textView);
}
}
}