Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- public class Database extends SQLiteOpenHelper {
- public Database(Context context) {
- super(context, "names", null, 1);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL("CREATE TABLE products (_id integer primary key autoincrement, "+ "name text, price integer)");
- }
- @Override
- public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
- }
- public void addProduct(String name, int price) {
- SQLiteDatabase db = getWritableDatabase();
- ContentValues row = new ContentValues();
- row.put("name", name);
- row.put("price", price);
- db.insert("products", null, row);
- }
- public Cursor getAllProducts() {
- SQLiteDatabase db = getReadableDatabase();
- return db.rawQuery("SELECT * FROM products", null);
- }
- public void deleteProduct(long id){
- SQLiteDatabase db = getWritableDatabase();
- //String string =String.valueOf(id);
- //database.execSQL("DELETE FROM favorite WHERE _id = '" + string + "'");
- String string =String.valueOf(id);
- db.execSQL("DELETE FROM products WHERE _id = '" + string + "'");
- //db.delete("products", String.format("_id=%d", id), null);
- }
- }
- ////////////////////////////////////////////////////////////////////////////////
- ADAPTER
- import android.content.Context;
- import android.database.Cursor;
- import android.view.View;
- import android.widget.SimpleCursorAdapter;
- public class Adapter extends SimpleCursorAdapter {
- @SuppressWarnings("deprecation")
- public Adapter(Context context, int layout, Cursor c, String[] from,
- int[] to) {
- super(context, layout, c, from, to);
- }
- @Override
- public void bindView(View view, Context context, Cursor cursor) {
- super.bindView(view, context, cursor);
- //something
- }
- }
- /////////////////////////////////////////////////////////////////////////
- peak_item
- <?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:orientation="horizontal" >
- <TextView
- android:id="@+id/textView1"
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:layout_weight="1"
- android:ems="10"
- android:hint="name" />
- <TextView
- android:id="@+id/textView2"
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:layout_weight="1"
- android:ems="10"
- android:hint="price"
- android:inputType="number" />
- <Button
- android:id="@+id/button1"
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:layout_weight="1"
- android:onClick="myClick"
- android:text="Button" />
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment