Created
February 26, 2023 16:46
-
-
Save biswa-rx/9ebd9013d62e8fb4fa255fec061912b1 to your computer and use it in GitHub Desktop.
SQLite RecyclerView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout 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" | |
android:padding="16dp" | |
tools:context="com.example.application.grocerylist.MainActivity"> | |
<EditText | |
android:id="@+id/edittext_name" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" /> | |
<TextView | |
android:id="@+id/textview_amount" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_below="@+id/edittext_name" | |
android:layout_centerHorizontal="true" | |
android:layout_margin="8dp" | |
android:text="0" | |
android:textSize="50sp" /> | |
<Button | |
android:id="@+id/button_decrease" | |
android:layout_width="50dp" | |
android:layout_height="wrap_content" | |
android:layout_alignTop="@+id/textview_amount" | |
android:layout_alignBottom="@+id/textview_amount" | |
android:layout_toStartOf="@+id/textview_amount" | |
android:text="-" /> | |
<Button | |
android:id="@+id/button_increase" | |
android:layout_width="50dp" | |
android:layout_height="wrap_content" | |
android:layout_alignTop="@+id/textview_amount" | |
android:layout_alignBottom="@+id/textview_amount" | |
android:layout_toEndOf="@+id/textview_amount" | |
android:text="+" /> | |
<Button | |
android:id="@+id/button_add" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignTop="@+id/button_increase" | |
android:layout_alignBottom="@+id/button_increase" | |
android:layout_marginStart="8dp" | |
android:layout_toEndOf="@+id/button_increase" | |
android:text="add" /> | |
<android.support.v7.widget.RecyclerView | |
android:id="@+id/recyclerview" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_below="@+id/textview_amount" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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="wrap_content" | |
android:layout_margin="8dp"> | |
<TextView | |
android:id="@+id/textview_amount_item" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="0" | |
android:textSize="30sp" /> | |
<TextView | |
android:id="@+id/textview_name_item" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginStart="8dp" | |
android:text="Empty Item" | |
android:textSize="30sp" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.application.grocerylist; | |
import android.content.Context; | |
import android.database.Cursor; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.TextView; | |
public class GroceryAdapter extends RecyclerView.Adapter<GroceryAdapter.GroceryViewHolder> { | |
private Context mContext; | |
private Cursor mCursor; | |
public GroceryAdapter(Context context, Cursor cursor) { | |
mContext = context; | |
mCursor = cursor; | |
} | |
public class GroceryViewHolder extends RecyclerView.ViewHolder { | |
public TextView nameText; | |
public TextView countText; | |
public GroceryViewHolder(View itemView) { | |
super(itemView); | |
nameText = itemView.findViewById(R.id.textview_name_item); | |
countText = itemView.findViewById(R.id.textview_amount_item); | |
} | |
} | |
@Override | |
public GroceryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
LayoutInflater inflater = LayoutInflater.from(mContext); | |
View view = inflater.inflate(R.layout.grocery_item, parent, false); | |
return new GroceryViewHolder(view); | |
} | |
@Override | |
public void onBindViewHolder(GroceryViewHolder holder, int position) { | |
if (!mCursor.moveToPosition(position)) { | |
return; | |
} | |
String name = mCursor.getString(mCursor.getColumnIndex(GroceryContract.GroceryEntry.COLUMN_NAME)); | |
int amount = mCursor.getInt(mCursor.getColumnIndex(GroceryContract.GroceryEntry.COLUMN_AMOUNT)); | |
long id = mCursor.getLong(mCursor.getColumnIndex(GroceryContract.GroceryEntry._ID)); | |
holder.nameText.setText(name); | |
holder.countText.setText(String.valueOf(amount)); | |
holder.itemView.setTag(id); | |
} | |
@Override | |
public int getItemCount() { | |
return mCursor.getCount(); | |
} | |
public void swapCursor(Cursor newCursor) { | |
if (mCursor != null) { | |
mCursor.close(); | |
} | |
mCursor = newCursor; | |
if (newCursor != null) { | |
notifyDataSetChanged(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.application.grocerylist; | |
import android.provider.BaseColumns; | |
public class GroceryContract { | |
private GroceryContract() { | |
} | |
public static final class GroceryEntry implements BaseColumns { | |
public static final String TABLE_NAME = "groceryList"; | |
public static final String COLUMN_NAME = "name"; | |
public static final String COLUMN_AMOUNT = "amount"; | |
public static final String COLUMN_TIMESTAMP = "timestamp"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.application.grocerylist; | |
import android.content.Context; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.database.sqlite.SQLiteOpenHelper; | |
import com.example.application.grocerylist.GroceryContract.*; | |
public class GroceryDBHelper extends SQLiteOpenHelper { | |
public static final String DATABASE_NAME = "grocerylist.db"; | |
public static final int DATABASE_VERSION = 1; | |
public GroceryDBHelper(Context context) { | |
super(context, DATABASE_NAME, null, DATABASE_VERSION); | |
} | |
@Override | |
public void onCreate(SQLiteDatabase db) { | |
final String SQL_CREATE_GROCERYLIST_TABLE = "CREATE TABLE " + | |
GroceryEntry.TABLE_NAME + " (" + | |
GroceryEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + | |
GroceryEntry.COLUMN_NAME + " TEXT NOT NULL, " + | |
GroceryEntry.COLUMN_AMOUNT + " INTEGER NOT NULL, " + | |
GroceryEntry.COLUMN_TIMESTAMP + " TIMESTAMP DEFAULT CURRENT_TIMESTAMP" + | |
");"; | |
db.execSQL(SQL_CREATE_GROCERYLIST_TABLE); | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | |
db.execSQL("DROP TABLE IF EXISTS " + GroceryEntry.TABLE_NAME); | |
onCreate(db); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.application.grocerylist; | |
import android.content.ContentValues; | |
import android.database.Cursor; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
import android.support.v7.widget.helper.ItemTouchHelper; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.TextView; | |
public class MainActivity extends AppCompatActivity { | |
private SQLiteDatabase mDatabase; | |
private GroceryAdapter mAdapter; | |
private EditText mEditTextName; | |
private TextView mTextViewAmount; | |
private int mAmount = 0; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
GroceryDBHelper dbHelper = new GroceryDBHelper(this); | |
mDatabase = dbHelper.getWritableDatabase(); | |
RecyclerView recyclerView = findViewById(R.id.recyclerview); | |
recyclerView.setLayoutManager(new LinearLayoutManager(this)); | |
mAdapter = new GroceryAdapter(this, getAllItems()); | |
recyclerView.setAdapter(mAdapter); | |
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, | |
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) { | |
@Override | |
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { | |
return false; | |
} | |
@Override | |
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { | |
removeItem((long) viewHolder.itemView.getTag()); | |
} | |
}).attachToRecyclerView(recyclerView); | |
mEditTextName = findViewById(R.id.edittext_name); | |
mTextViewAmount = findViewById(R.id.textview_amount); | |
Button buttonIncrease = findViewById(R.id.button_increase); | |
Button buttonDecrease = findViewById(R.id.button_decrease); | |
Button buttonAdd = findViewById(R.id.button_add); | |
buttonIncrease.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
increase(); | |
} | |
}); | |
buttonDecrease.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
decrease(); | |
} | |
}); | |
buttonAdd.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
addItem(); | |
} | |
}); | |
} | |
private void increase() { | |
mAmount++; | |
mTextViewAmount.setText(String.valueOf(mAmount)); | |
} | |
private void decrease() { | |
if (mAmount > 0) { | |
mAmount--; | |
mTextViewAmount.setText(String.valueOf(mAmount)); | |
} | |
} | |
private void addItem() { | |
if (mEditTextName.getText().toString().trim().length() == 0 || mAmount == 0) { | |
return; | |
} | |
String name = mEditTextName.getText().toString(); | |
ContentValues cv = new ContentValues(); | |
cv.put(GroceryContract.GroceryEntry.COLUMN_NAME, name); | |
cv.put(GroceryContract.GroceryEntry.COLUMN_AMOUNT, mAmount); | |
mDatabase.insert(GroceryContract.GroceryEntry.TABLE_NAME, null, cv); | |
mAdapter.swapCursor(getAllItems()); | |
mEditTextName.getText().clear(); | |
} | |
private void removeItem(long id) { | |
mDatabase.delete(GroceryContract.GroceryEntry.TABLE_NAME, | |
GroceryContract.GroceryEntry._ID + "=" + id, null); | |
mAdapter.swapCursor(getAllItems()); | |
} | |
private Cursor getAllItems() { | |
return mDatabase.query( | |
GroceryContract.GroceryEntry.TABLE_NAME, | |
null, | |
null, | |
null, | |
null, | |
null, | |
GroceryContract.GroceryEntry.COLUMN_TIMESTAMP + " DESC" | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment