android


Delete row in Custom Listview using SQLITE


I am creating a shopping cart in my app. I am using sqlite to store the values in a table to retrieve what the person added.
I have added a button for each row, so that when they click that button it will remove the row from the list and its data from the table.
But how do I call my delete function when the button is clicked? Thanks in advance!
this is my Adapter class:
public class ListCartAdapter extends BaseAdapter {
private Context context;
private ArrayList<String> orderid;
private ArrayList<String> orderName;
private ArrayList<String> orderSize;
private ArrayList<String> orderQuantity;
public ListCartAdapter(Context context, ArrayList<String> orderid, ArrayList<String> orderName, ArrayList<String> orderSize, ArrayList<String> orderQuantity){
// public ListCartAdapter(Context context, ArrayList<String> orderName){
this.context = context;
this.orderid = orderid;
this.orderName = orderName;
this.orderSize = orderSize;
this.orderQuantity = orderQuantity;
}
#Override
public int getCount() {
return orderName.size();
}
#Override
public Object getItem(int position) {
return orderName.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View listView;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
listView = inflater.inflate(R.layout.cart_list_item, null);
TextView number = (TextView) listView.findViewById(R.id.textID);
TextView name = (TextView) listView.findViewById(R.id.textOrderName);
TextView size = (TextView) listView.findViewById(R.id.textOrderSize);
TextView quantity = (TextView) listView.findViewById(R.id.textOrderQuantity);
ImageButton btnRemove = (ImageButton) listView.findViewById(R.id.btnRemove);
number.setText(orderid.get(position));
name.setText(orderName.get(position));
size.setText(orderSize.get(position));
quantity.setText(orderQuantity.get(position));
//BUTTON TO REMOVE ROW
btnRemove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
orderName.remove(position);
notifyDataSetChanged();
}
});
return listView;
}
and this is My SQLITEHELPER CLASS:
public static final String DB_NAME = "CartDB";
public static final String TABLE_NAME = "Orders";
public static final String COLUMN_ID = "id";
public static final String NAME ="name";
public static final String SIZE ="size";
public static final String QUANTITY ="quantity";
private static final int DB_VERSION = 1;
public cartDatabaseHelper(Context context)
{
super(context,DB_NAME,null,DB_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE " + TABLE_NAME
+ "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ NAME + " VARCHAR, "
+ SIZE + " VARCHAR, "
+ QUANTITY + " VARCHAR);";
db.execSQL(sql);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "DROP TABLE IF EXIST Orders";
db.execSQL(sql);
onCreate(db);
}
public boolean addPerson(String name, String size, String quantity){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(NAME,name);
contentValues.put(SIZE,size);
contentValues.put(QUANTITY,quantity);
long result = db.insert(TABLE_NAME,null ,contentValues);
if(result == -1)
return false;
else
return true;
}
public Cursor getListContents(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
return data;
}
public void removeSingleContact(String id) {
SQLiteDatabase database = this.getWritableDatabase();
database.execSQL("DELETE FROM " + TABLE_NAME + " WHERE " + COLUMN_ID + "= '" + id + "'");
//Close the database
database.close();
}
Well I solved my question :D I still don't know why I was downvoted, but who cares.
This is the way to call your SqliteHelper in your Adapter class, add this code
final cartDatabaseHelper db = new cartDatabaseHelper(context);
I hope this helps other people with this problem :D

Related Links

Make CalendarView in android with selection of specific dates from limited number
Android - recyclerAdapter.getItem returns null in the onClick callback
I can't change the visibility of a RecyclerView
How to get 2 columns in ListView using SimpleCursor Adapter in Android?
How can I get the value of charging current (in mA) on android API level 16?
Selecting each record from JSON result and then adding it to listview , Android
Do android APKs include any identifying or user data
ExoPlayer InvalidResponseCodeException: Response code: 503 audio streaming
Strange behavior in animation when loading more items into recyclerview
setMyLocationEnabled(true) shows blue dot but not MyLocation button
Multiple Instances and Multiple Activities calling the same Activity
how to fix “Failed to install 'cordova-plugin-console':Error ”
viewholder not receiving clicks - android
Compilation failed app:compilieDebugJavaWithJavac
Android - sent ImageView resource through intent
How to achieve half circular reveal animation in android?

Categories

HOME
eclipse
pagination
angular-formly
components
requirements
rendering
rds
dojo
big-o
android-json
google-admin-sdk
flume
comsol
html5-appcache
sigma.js
simpleitk
breadth-first-search
poedit
imagemap
react-toolbox
highstock
pic32
opentk
interbase
progress-bar
firebase-analytics
subclass
comparable
tizen-tv
calculation
eviews
hard-drive
pop3
installscript
html-encode
reverse
processwire
numpy-broadcasting
salesforce-communities
playframework-2.1
css-position
linq-to-objects
unordered-map
oracle-service-bus
builder
configurationmanager
static-cast
edit
django-1.10
branch-prediction
testrail
stripe-connect
atlas
actor-platform
google-cloud-console
async.js
windows-azure-queues
eigenvector
jtwig
nservicebus5
dbamp
yajsw
jaunt-api
concurrentmodification
decimalformat
iokit
opserver
cgi-bin
viola-jones
dnssec
xmltype
rapidsvn
sip-server
genymotion-call
mdichild
create.js
terminate
amslidemenu
conditionaltagsupport
windows-embedded-standard
high-resolution
rfc5322
gwt-openlayers
jeromq
ui-select2
nebula
video-codecs
swfobject
paginator
thttpd
floating-point-exceptions
ikimagebrowserview
htdocs
google-ajax-api
delimited-text
non-clustered-index
easyb
openfaces
cldc
alsb
xpsdocument

Resources

Encrypt Message