katjet0

database + adapter + peak_item

Mar 23rd, 2013
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.14 KB | None | 0 0
  1. import android.content.ContentValues;
  2. import android.content.Context;
  3. import android.database.Cursor;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.database.sqlite.SQLiteOpenHelper;
  6.  
  7. public class Database extends SQLiteOpenHelper {
  8.  
  9.     public Database(Context context) {
  10.         super(context, "names", null, 1);
  11.     }
  12.  
  13.     @Override
  14.     public void onCreate(SQLiteDatabase db) {
  15.         db.execSQL("CREATE TABLE products (_id integer primary key autoincrement, "+ "name text, price integer)");
  16.     }
  17.  
  18.     @Override
  19.     public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
  20.     }
  21.    
  22.     public void addProduct(String name, int price) {
  23.         SQLiteDatabase db = getWritableDatabase();     
  24.         ContentValues row = new ContentValues();
  25.         row.put("name", name);
  26.         row.put("price", price);
  27.         db.insert("products", null, row);
  28.     }
  29.  
  30.     public Cursor getAllProducts() {
  31.         SQLiteDatabase db = getReadableDatabase();
  32.         return db.rawQuery("SELECT * FROM products", null);
  33.     }
  34.    
  35.     public void deleteProduct(long id){
  36.         SQLiteDatabase db = getWritableDatabase();
  37.         //String string =String.valueOf(id);
  38.         //database.execSQL("DELETE FROM favorite WHERE _id = '" + string + "'");
  39.         String string =String.valueOf(id);
  40.         db.execSQL("DELETE FROM products WHERE _id = '" + string + "'");
  41.         //db.delete("products", String.format("_id=%d", id), null);
  42.     }
  43.  
  44. }
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55. ////////////////////////////////////////////////////////////////////////////////
  56. ADAPTER
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64. import android.content.Context;
  65. import android.database.Cursor;
  66. import android.view.View;
  67. import android.widget.SimpleCursorAdapter;
  68.    
  69. public class Adapter extends SimpleCursorAdapter {
  70.  
  71.    
  72.     @SuppressWarnings("deprecation")
  73.     public Adapter(Context context, int layout, Cursor c, String[] from,
  74.         int[] to) {
  75.         super(context, layout, c, from, to);
  76.     }
  77.    
  78.     @Override
  79.     public void bindView(View view, Context context, Cursor cursor) {
  80.         super.bindView(view, context, cursor);
  81.        
  82.         //something
  83.        
  84.     }
  85. }
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100. /////////////////////////////////////////////////////////////////////////
  101. peak_item
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108. <?xml version="1.0" encoding="utf-8"?>
  109. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  110.     android:layout_width="match_parent"
  111.     android:layout_height="wrap_content"
  112.     android:orientation="horizontal"  >
  113.  
  114.         <TextView
  115.             android:id="@+id/textView1"
  116.             android:layout_width="match_parent"
  117.             android:layout_height="50dp"
  118.             android:layout_weight="1"
  119.             android:ems="10"
  120.             android:hint="name" />
  121.  
  122.         <TextView
  123.             android:id="@+id/textView2"
  124.             android:layout_width="match_parent"
  125.             android:layout_height="50dp"
  126.             android:layout_weight="1"
  127.             android:ems="10"
  128.             android:hint="price"
  129.             android:inputType="number" />
  130.  
  131.         <Button
  132.             android:id="@+id/button1"
  133.             android:layout_width="match_parent"
  134.             android:layout_height="50dp"
  135.             android:layout_weight="1"
  136.             android:onClick="myClick"
  137.             android:text="Button" />
  138.  
  139. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment