What is the Best way to handle a lot of data on a Recycleview

Viewed 3627

I need to load a long list of data (5000-1000) on a RecycleView, and each items of the RecycleView has many details, so 5 TextView, 1 ImageView and a ProgressBar. All the data are stored on the SQLite DB. Is it better to load an ArrayList<Object> which contains all the data and them set it to the adapter or is it better to load the data on the adapter? Please consider I have other thing to do on the adapter like retriving the images (with Glide). Any suggestion or consideration would be helpful!

Thank you

4 Answers

you can achieve this by Android Room & Paging component i have try load data record count>10m

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.setViewModel(new ViewModelProvider(this).get(ViewModel.class));
        binding.setLifecycleOwner(this);
    }


    public static class ViewModel extends AndroidViewModel {
        public final List<Contact> contactList = new ArrayList<>();
        private final ContactDatabase database = Room.databaseBuilder(getApplication(), ContactDatabase.class, "contact.db")
                .fallbackToDestructiveMigration()
                .allowMainThreadQueries()
                .build();
        public final LiveData<PagedList<Contact>> pagedListLiveData = new LivePagedListBuilder<>(database.getContactDao().getAllContact3(), 1000).build();

        public ViewModel(@NonNull Application application) {
            super(application);
        }
    }
}

Room Database Entity class

@Entity
public class Contact {

    @PrimaryKey(autoGenerate = true)
    public int _id;

    public int _accountId;

    public int photo_id;

    public String display_name;
    public String display_name_alt;
    public String display_name_source;

    public int starred;
    public int pinned;

    public String phonetic_name;
    public String phonetic_name_style;

    public int deleted;
    public int dirty;
}

Room Database Dao

@Dao
public interface ContactDao {

    @Query("SELECT * FROM contact")
    List<Contact> getAllContact();

    @Query("SELECT * FROM contact")
    LiveData<List<Contact>> getAllContact2();

    @Insert
    void insertAll(List<Contact> contacts);

    @Query("SELECT * FROM contact")
    DataSource.Factory<Integer, Contact> getAllContact3();

    @Query("SELECT * FROM contact")
    PositionalDataSource<Contact> getAllContact4();

}

Layout

<?xml version="1.0" encoding="utf-8"?>
<layout>

    <data>

        <variable
            name="viewModel"
            type="com.xxx.hugedatabaserecordtest.MainActivity.ViewModel" />
    </data>

    <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.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            app:itemTemplate="@{@layout/contact_list_item_template}"
            app:items="@{viewModel.pagedListLiveData}"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Related