How to update RecyclerView Adapter Data?

AndroidMobile DevelopmentApps/Applications

This example demonstrate about How to update RecyclerView Adapter Data

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   android:orientation = "vertical"
   android:layout_width = "match_parent"
   android:gravity = "center"
   android:layout_height = "wrap_content">
   <android.support.v7.widget.RecyclerView
      android:id = "@+id/recycler_view"
      android:layout_width = "match_parent"
      android:layout_height = "400dp"
      android:scrollbars = "vertical" />
   <TextView
      android:id = "@+id/text"
      android:textSize = "30sp"
      android:text = "Update"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content" />
</LinearLayout>

In the above code, we have taken recycerview.

Step 3 − Add the following code to src/MainActivity.java

<?xml version = "1.0" encoding = "utf-8"?>
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.content.pm.ShortcutInfoCompat;
import android.support.v4.content.pm.ShortcutManagerCompat;
import android.support.v4.graphics.drawable.IconCompat;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;

public class MainActivity extends Activity {
   private RecyclerView recyclerView;
   private customAdapter mAdapter;
   TextView text;
   ArrayList<String> list = new ArrayList<>();
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
      RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
      recyclerView.setLayoutManager(mLayoutManager);
      recyclerView.setItemAnimator(new DefaultItemAnimator());
      mAdapter = new customAdapter(this,list);
      recyclerView.setAdapter(mAdapter);
      recyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL));
      list.add("sairamm");
      list.add("Krishna");
      list.add("prasad");
      text = findViewById(R.id.text);
      text.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            list.add("Mammahe");
            mAdapter.notifyDataSetChanged();
         }
      });
   }
}

Step 4 − Add the following code to src/ customAdapter.java

<?xml version = "1.0" encoding = "utf-8"?>
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;

public class customAdapter extends RecyclerView.Adapter<customAdapter.MyViewHolder> {
   Context context;
   ArrayList<String> list;
   public class MyViewHolder extends RecyclerView.ViewHolder {
      public TextView title;
      public MyViewHolder(View view) {
         super(view);
         title = (TextView) view.findViewById(R.id.title);
      }
   }
   public customAdapter(Context context, ArrayList<String> list) {
      this.context = context;
      this.list = list;
   }
   @NonNull
   @Override
   public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
      View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row, viewGroup, false);
      return new MyViewHolder(itemView);
   }
   @Override
   public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
      myViewHolder.title.setText(list.get(i));
   }
   @Override
   public int getItemCount() {
      return list.size();
   }
}

Step 5 − Add the following code to res/layout/ list_row.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   android:orientation = "vertical" android:layout_width = "match_parent"
   android:layout_height = "wrap_content">
   <TextView
      android:id = "@+id/title"
      android:layout_width = "wrap_content"
      android:textSize = "30sp"
      android:layout_marginLeft = "10dp"
      android:layout_height = "wrap_content" />
</LinearLayout>

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run  icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen –

Now click on textview, it will show result as follows

raja
Published on 12-Apr-2019 16:09:18
Advertisements