How To Get Data From Cursor To A Recyclerview
SQLite with Recycler View
Awalnya jika ingin menampilkan data dalam jumlah banyak, developer akan menggunakan ListView atau GridView. Namun, saat ini penggunaan ListView dan GridView sudah mulai ditinggalkan. Hal ini dikarenakan adanya metode baru yang lebih unggul yaitu RecyclerView. Berikut beberapa keunggulan dari Recycler View:
- Neat and Structured (Rapi dan Terstruktur), sehingga akan mempercepat performa sistem dalam mengeksekusi kode.
- Implement View Holder (Menggunakan ViewHolder), umumnya RecyclerView akan menyimpan setiap variabel dalam sebuah ViewHolder. ViewHolder adalah form static yang ada dalam Adapter berfungsi untuk menyimpan view-view yang digunakan untuk menampilkan satu particular information. Penggunaan ViewHolder ini akan mengurangi waktu eksekusi jika dibandingkan dengan findViewById() saat mengupdate data.
- Implement Adapter (Menggunakan Adapter), umumnya RecyclerView akan menggunakan Adapter untuk mengatur konten apa saja yang nantinya akan ditampilkan di list item. Penggunaan Adapter ini akan memudahkan programmer dalam mengcustom list item, tentunya akan mengurangi waktu eksekusi sistem dan mengurangi line of lawmaking.
Contoh Implementasi SQLite — RecyclerView
Pada sistem ini akan dibuat sistem yang menampilkan data siswa dengan particular id, nama, dan isBookmark dengan menggunakan RecyclerView.
- BookmarkActivity.class
public class BookmarkActivity extends Activity{ private SQLiteDatabaseHandler db;
private List<Student> listStudent = new ArrayList<Student>();
individual RecyclerView recyclerView;
individual BookmarkAdapter adapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout. activity_bookmark );
db = new SQLiteDatabaseHandler(this);
recyclerView = (RecyclerView) findViewById(R.id. rv_bookmark );
Educatee student1 = new Student("1", "Ahmad", "1");
Educatee student2 = new Student("2", "Bimo", "0");
Student student3 = new Educatee("3", "Cici", "0");
Educatee student4 = new Educatee("4", "Danang", "0");
Student student5 = new Student("5", "Ella", "1");
db.addPlayer(student1);
db.addPlayer(student2);
db.addPlayer(student3);
db.addPlayer(student4);
db.addPlayer(student5);
listStudent.addAll(db.allPlayers());
adapter = new BookmarkAdapter(this, listStudent);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());DividerItemDecoration decoration = new DividerItemDecoration(getApplicationContext(), VERTICAL );
recyclerView.addItemDecoration(decoration); recyclerView.setAdapter(adapter);
}
}
- activity_bookmark.xml
<? xml version="1.0" encoding="utf-8" ?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Semua Information Siswa"/>
<android.back up.v7.widget.RecyclerView
android:id="@+id/rv_bookmark"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
- SQLiteDatabaseHelper.java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; import java.util.ArrayList;
import coffee.util.Listing;
public form SQLiteDatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "StudentDB";
individual static final Cord TABLE_NAME = "STUDENT";
private static final Cord KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_ISBOOKMARK = "isBookmark";
private static final String[] COLUMNS = { KEY_ID , KEY_NAME , KEY_ISBOOKMARK };
public SQLiteDatabaseHandler(Context context) {
super(context, DATABASE_NAME , cipher, DATABASE_VERSION );
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATION_TABLE = "CREATE TABLE STUDENT ( "
+ "id INTEGER PRIMARY Primal AUTOINCREMENT, "
+ "proper noun TEXT, "
+ "isBookmark TEXT)";
db.execSQL(CREATION_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// you can implement here migration process
db.execSQL("DROP Tabular array IF EXISTS " + TABLE_NAME );
this.onCreate(db);
}
public List<Student> allPlayers() {
List<Pupil> students = new ArrayList<>();
Cord selectQuery = "SELECT * FROM Educatee";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, nix);
if (cursor.moveToFirst()) {
practice {
Student student = new Student();
student.setId(cursor.getString(0));
student.setName(cursor.getString(ane));
student.setIsBookmark(cursor.getString(ii));
students.add(student);
} while (cursor.moveToNext());
}
db.shut();
render students;
}
}
- BookmarkAdapter
import android.content.Context;
import android.back up.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List; public course BookmarkAdapter extends RecyclerView.Adapter<BookmarkAdapter.MyViewHolder> {
private Context context;
private Listing<Educatee> notesList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView tvName;
public TextView tvId;
public MyViewHolder(View view) {
super(view);
tvName = view.findViewById(R.id. tv_name );
tvId = view.findViewById(R.id. tv_id );
}
}
public BookmarkAdapter(Context context, List<Student> notesList) {
this.context = context;
this.notesList = notesList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout. listview_bookmark , parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
terminal Student educatee = notesList.become(position);
holder.tvName.setText(student.getName());
holder.tvId.setText(pupil.getId());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, pupil.getName(), Toast. LENGTH_SHORT ).testify();
}
});
}
@Override
public int getItemCount() {
return notesList.size();
}
}
- listview_bookmark.xml
<? xml version="ane.0" encoding="utf-8" ?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="truthful"
android:foreground="?attr/selectableItemBackground"
android:padding="10dp"> <TextView
android:id="@+id/tv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp" />
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorAccent"
android:layout_toRightOf="@id/tv_id" />
</RelativeLayout>
- Result
How To Get Data From Cursor To A Recyclerview,
Source: https://silsly.medium.com/sqlite-with-recycler-view-845cf67e0e8e
Posted by: bustillosclaill1953.blogspot.com
0 Response to "How To Get Data From Cursor To A Recyclerview"
Post a Comment