Display and Swipe-to-Delete Data in RecycleView using Cursor
For this blog let assume, I already have the class to interact with sqlite database (Contract, DBHelpler, ContentProvider Classes). Now let start with showing the data using recycleview. I am going to create a todo list app so you will find alot of variables that refer to terms as task/todo list etc. in another article i will be adding its sqlite database class for reference if anyone wants to refere. but here you only find how to display and swipe the data.
5. Now, extends the 'TodoListAdapater' class with RecyclerView.Adapter
Display Data in RecycleView
1. Update the UI in the MainActivity.xml file
2. Add new layout resource file with name 'todo_list_item.xml'
Right click on layout folder and select 'new' then select 'layout resource file'. Type the name as 'todo_list_item' and select the root element 'Linear Layout'. and lastly click Ok.
3. Create a new class 'TodoListAdapter'
Before creating a new class, i am going to create a new folder name 'Adapters'. Then right click on adapter folder and create a new class 'TodoListAdapater'.
4. Now create a subclass (in 'TodoListAdapater' class) 'TodoListViewHolder' that extends RecycleView.ViewHolder
now add constructor method TodoListViewHolder() where create the textviews object which holds information to display. after that create a bind() which set the value in the textviews. the bind methods take three parameters such as task name, task priority and task status.
5. Now, extends the 'TodoListAdapater' class with RecyclerView.Adapter
In this class we overrides three methods onCreateViewHolder(), onBindViewHolder(), getItemCount(), and create constructor TodoListAdapter() method and another method which takes cursor as a parameter called swapCursor().
To start with initialize two private variables
1. private Cursor mCursor;
2. private Context mContext;
Define constructor which takes Context as a parameter then set value in mContext variable.
Then override onCreateViewholder method, which inflate the layout resource we create earlier 'todo_list_item.xml' and pass the view in the subclass 'TodoListViewHolder' object. and return the class object.
In onBindViewHolder() method, we read from cursor the desired row and pass the value to the TodoListViewHolder class bind() method we define earlier.
In getItemCount() method, we return number of rows in the cursor by calling mCursor.getCount() method. and in the swapCursor method we take cursor object as parameter then set the cursor object with cursor we define as mCursor.
Now we are ready to combine the code in our MainActivity.java file to display data in our app.
Now you are ready to execute your app first time. hope it will run smoothly. The next part of the article talks about how to add swipe functionality in the data rows. So now I am going to write the code please follow correctly.
The ItemTouchHelper class helps you to add the swipe / drag-drop functionality with recycleview.
Ok, now we are good to go to run the app and see the swipe functionality in the app. let try to run the app hope it runs smoothly.
Now, if you want to add the custom icon and background color you need to modify the onChildDraw() method.
To download the sample code Click here
Now we are ready to combine the code in our MainActivity.java file to display data in our app.
6. Update MainActivity.java to display data in list
First, Initialize RecyclyeView , TodoListAdapater ,Paint variables as private to the class. Then modify onCreate() method to create RecycleView object and set TodoListAdapater object to the recycleview.
MainActivity.java class also implements LoaderManager.LoadersCallback interface and implements the methods to fill adapater with data which is fetch from sqlite database.
Now, implements the Loaders methods start with onCreateLoader which returns a Cursor Array
now implement remain two onLoadFinish() and onLoaderReset()
Now you are ready to execute your app first time. hope it will run smoothly. The next part of the article talks about how to add swipe functionality in the data rows. So now I am going to write the code please follow correctly.
Swipe LEFT | RIGHT in RecycleView to Perform CURD operations
I am adding swipe function to delete the record and update the record status.
1. Add deleteRecord and updateRecord methods in TodoListAdapater class
2. Update MainActivity class
Now go back to MainActivity.java file and modify onCreate() method - adding following lines of code just after setting the adapater to recycleview object.The ItemTouchHelper class helps you to add the swipe / drag-drop functionality with recycleview.
Ok, now we are good to go to run the app and see the swipe functionality in the app. let try to run the app hope it runs smoothly.
Now, if you want to add the custom icon and background color you need to modify the onChildDraw() method.
To download the sample code Click here











Comments
Post a Comment