Recyclerview Shows Previous Values Entered In An Edittext In New Rows
I'm creating an android app, in which I'm using recyclerView and the row of recyclerView is having editText. This is my ReadingAdapter class public class ReadingAdapter extends Rec
Solution 1:
RecyclerView
reuse views, in fact it only generate the as many as views that is visible on the screen. so it's expected if you can see a value you set for other rows
The solution would be set all attributes of the view that you are changing to default or whatever the row should present from your data set
So put addTextChangedListener
insode ViewHolder
constructor(you can get position by calling getAdapterPosition()
) for better performance and set the editText
value inside onBindViewHolder
method from your data set
Solution 2:
Your Activity Code:
ListViewlistview= (ListView) findViewById(R.id.list_view);
listview.setItemsCanFocus(true);
Adapteradapter=newAdapter (YourActivity.this, YourArrayList);
listview .setAdapter(adapter);
Adapter class
publicclassAdapterextendsBaseAdapter {
// Declare Variables \\Context mContext;
LayoutInflater inflater;
Activity act;
String[] temp;
publicAdapter(Context context, ArrayList<String> list) {
mContext = context;
inflater = LayoutInflater.from(mContext);
act = (Activity) context;
//-------Temp String Array-------\\
temp = newString[this.count];
for (int i = 0; i < this.count; i++) {
temp[i] = list.get(i);
}
//---------------------------\\
}
publicclassViewHolder {
TextView optionTitle;
EditText optionText;
int ref;
}
@Overridepublic int getCount() {
return list.size;
}
@OverridepublicObjectgetItem(int position) {
return temp[position];
}
@Overridepublic long getItemId(int position) {
return position;
}
publicViewgetView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = newViewHolder();
view = inflater.inflate(R.layout.lv_items_add_ques_options_mcq, null);
holder.optionTitle = (TextView) view.findViewById(R.id.add_ques_opts_count_mcq_tv);
holder.optionText = (EditText) view.findViewById(R.id.add_ques_opts_title_mcq_et);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.ref = position;
holder.optionTitle.setText(getCharForNumber(position) + ":");
holder.optionText.setText(temp[position]);
holder.optionText.addTextChangedListener(newTextWatcher() {
@OverridepublicvoidonTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@OverridepublicvoidbeforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@OverridepublicvoidafterTextChanged(Editable arg0) {
temp[holder.ref] = arg0.toString().trim();
}
});
return view;
}
publicvoidgetList() {
StaticValues.arrayListOptions = newArrayList<String>(Arrays.asList(temp));
StaticValues.arrayListOptionsCount = newArrayList<String>();
for (int i = 0; i < count; i++) {
StaticValues.arrayListOptionsCount.add(String.valueOf(i+1));
Log.e("err_al", StaticValues.arrayListOptions.get(i));
Log.e("err_al", StaticValues.arrayListOptionsCount.get(i));
}
}
privateStringgetCharForNumber(int i) {
char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
if (i > 25) {
returnnull;
}
returnCharacter.toString(alphabet[i]);
}}
Post a Comment for "Recyclerview Shows Previous Values Entered In An Edittext In New Rows"