0

My recyclerview data changed while I scrolling down/up. when I scroll the layout it appears every time with new values I've added both methods and as well and false recyclable too. But, didn't work out.

Here's is how my adapter looks like.

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

Context context;
ArrayList<KcResponse> kcList;
String birthDate; }

 @NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_client_detail, parent, false);
    DetailListAdapter.ViewHolder vh = new DetailListAdapter.ViewHolder(v);
    return vh;
}


@Override
public void onBindViewHolder(@NonNull DetailListAdapter.ViewHolder holder, int position) {

    holder.setIsRecyclable(false);
    holder.periodTv.setText(kcList.get(position).getPeriod());

  TextView textView1 = new TextView(context);
    textView1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));

    for (int i = 0; i < arr.length; i++) {
        String line = arr[i];
        SpannableString ss = new SpannableString(line);
        ss.setSpan(new BulletSpan(bulletGap), 0, line.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ssb.append(ss);

        //avoid last "\n"
        if (i + 1 < arr.length)
            ssb.append("\n");

    }

    textView1.setTextSize(12);
    textView1.setTextColor(ContextCompat.getColor(context, R.color.black_text));
    textView1.setPadding(15, 0, 0, 0);
    textView1.setText(ssb); // bullet text
    holder.effectsLl.addView(textView1);

   holder.periodTv.setBackgroundColor(Color.parseColor(kcList.get(position).getColor()));}

 @Override
public int getItemCount() {
    return kcList.size();
}
Himani
  • 440
  • 1
  • 6
  • 17

3 Answers3

1

You do not have more than one type of views in your recylcerview so one thing for sure that you do not need to override getItemViewType(int position).

Also the usage of method getItemId(int position) is incorrect. That method is used to get the stable ID for the item at position.

buzzingsilently
  • 1,546
  • 3
  • 12
  • 18
0

Add this overriding method to your custom adapter

 @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }
Ak23
  • 342
  • 1
  • 5
  • 17
-1

The adapter code you shared is not enough to identify the issue. So I am sharing a very simple recyclerview and its adapter code, just try to use that. I hope it will help you.

Happy coding...

    public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private DataAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = findViewById(R.id.recyclerView);
        adapter = new DataAdapter();

        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new DividerItemDecoration(this));
    }
}

Adapter class code

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

    public DataAdapter() {
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View listItem = layoutInflater.inflate(R.layout.adapter_layout, parent, false);
        ViewHolder viewHolder = new ViewHolder(listItem);
        return viewHolder;
    }

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

        holder.textView.setText("TextView1");
        holder.textView.setText("TextView2");
    }


    @Override
    public int getItemCount() {
        return 20;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView imageView;
        public TextView textView;

        public ViewHolder(View itemView) {
            super(itemView);
            this.imageView = itemView.findViewById(R.id.textView1);
            this.textView = (TextView) itemView.findViewById(R.id.textView2);
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#f1f1f1"
        tools:context=".MainActivity">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

adapter_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="4dp"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView1" />

</androidx.constraintlayout.widget.ConstraintLayout>
user320676
  • 394
  • 2
  • 19