'Add the edit data function in recycler view using interfaces

My code is working fine, it adds the data when refresh the page and it deletes the data on long press, but when I refresh the page duplicates the same data that was added before secondly I want to add the edit function in edit data button but I have no idea how to do this. Geeks help me to sort this issue.

MAIN ACTIVITY:

public class MainActivity extends AppCompatActivity implements ExampleInterface{
RecyclerView recyclerView;
RecyclerAdapter recyclerAdapter;

List<String> moviesList;

SwipeRefreshLayout swipeRefreshLayout;

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

    moviesList=new ArrayList<>();

    recyclerView= findViewById(R.id.recyclerView);
    recyclerAdapter=new RecyclerAdapter(moviesList,this);

    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    recyclerView.setAdapter(recyclerAdapter);

    DividerItemDecoration dividerItemDecoration=new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
    recyclerView.addItemDecoration(dividerItemDecoration);


    moviesList.add("Iron Man");
    moviesList.add("Iron men");
    moviesList.add("Iron Mann");
    moviesList.add("Iron Many");
    moviesList.add("Iron Manv");
    moviesList.add("Iron Mane");
    moviesList.add("Iron Manr");
    moviesList.add("Iron Manee");
    moviesList.add("Iron Manw");
    moviesList.add("Iron Mana");
    moviesList.add("Iron Mana");
    moviesList.add("Iron Manc");
    moviesList.add("Iron Manx");
    moviesList.add("Iron Manz");
    moviesList.add("Iron Mann");

    swipeRefreshLayout =findViewById(R.id.swiperefreshlayout);
    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            moviesList.add("Black widows(2022)");
            moviesList.add("Black widow(2022)");
            moviesList.add("Black widow(2020)");
            moviesList.add("Black widow(2019)");
            moviesList.add("Black widow(2018)");
            recyclerAdapter.notifyDataSetChanged();
            swipeRefreshLayout.setRefreshing(false);
        }
    });



}

@Override
public void onItemClick(int position) {

    Toast.makeText(this,moviesList.get(position),Toast.LENGTH_SHORT).show();


}

@Override
public void onLongItemClick(int position) {

    moviesList.remove(position);
    recyclerAdapter.notifyItemRemoved(position);

}
@Override
public void editData(int position){

    moviesList.remove(position);
    recyclerAdapter.notifyItemRemoved(position);

}}

Adapter class:

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.viewHolder>{
private static final String TAG="RecyclerAdapter";

List<String> moviesList;
private ExampleInterface exampleInterface;

public RecyclerAdapter(List<String> moviesList,ExampleInterface exampleInterface) {
    this.moviesList = moviesList;
    this.exampleInterface=exampleInterface;
}

@NonNull
@Override



public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater layoutInflater=LayoutInflater.from(parent.getContext());
    View view=layoutInflater.inflate(R.layout.row_item, parent,false);

    viewHolder viewHolder= new viewHolder(view);

    return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull viewHolder holder, int position) {
holder.rowCountTextview.setText(String.valueOf(position));
holder.textView.setText(moviesList.get(position));

}

@Override
public int getItemCount() {
    return moviesList.size();
}

class viewHolder extends RecyclerView.ViewHolder{

    ImageView imageview;
    TextView textView,rowCountTextview;
    Button btn;

    public viewHolder(@NonNull View itemView) {
        super(itemView);

        imageview=itemView.findViewById(R.id.imageView);
        textView=itemView.findViewById(R.id.textView);
        rowCountTextview=itemView.findViewById(R.id.rowcounttextview);
        btn=itemView.findViewById(R.id.button);




        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                exampleInterface.onItemClick(getAdapterPosition());
            }
        });


        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                exampleInterface.onLongItemClick(getAdapterPosition());

            }
        });




        itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {

               /* moviesList.remove(getAdapterPosition());
                notifyItemRemoved(getAdapterPosition());*/

                exampleInterface.onLongItemClick(getAdapterPosition());
                return true;


            }
        });


    }


}}

INTERFACE CLASS

public interface ExampleInterface {void  onItemClick( int position);

void onLongItemClick(int position);void editData(int position);}

MAINACTIVITY.XML

<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" tools:context=".MainActivity">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:id="@+id/swiperefreshlayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" >


    </androidx.recyclerview.widget.RecyclerView>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout></androidx.constraintlayout.widget.ConstraintLayout>

ROW_ITEM.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">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="90dp"
        android:layout_height="82dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:background="@color/black"
        android:layout_marginBottom="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_launcher_foreground" />



    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="12dp"
        android:text="TextView"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        app:layout_constraintBottom_toTopOf="@+id/rowcounttextview"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/imageView"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/rowcounttextview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textAppearance="@style/TextAppearance.AppCompat.Small"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/textView"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <Button
        android:id="@+id/button"
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="EDIT DATA"
        android:textSize="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/rowcounttextview"
        app:layout_constraintVertical_bias="1.0"
        tools:layout_editor_absoluteX="196dp" />
</androidx.constraintlayout.widget.ConstraintLayout>


Solution 1:[1]

Question 1:

The documentation is kinda unclear, but I guess using setRefreshing(false) is not enough.

Notify the widget that refresh state has changed. Do not call this when refresh is triggered by a swipe gesture.

@param refreshing Whether or not the view should show refresh progress.

So try adding setEnabled(false).

Question 2:

Use the method set(int index, String element) of ArrayList<String>.

If you want to ask your user for a new string to insert there, you could use an AlertDialog (popup) and pass an EditText to setView. When the user clicks the OK-button, you can obtain the text using EditText#getText().toString().

Use recyclerAdapter.notifyDataSetChanged() to refresh.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cactusroot