Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have made few minor changes with your code. You can use the same for reference.</p> <p>Modify the below according to your requirements. I had a similar sample and i modified the same with your code. I am not sure if this is the best way. But you can use the below and modify the same according to your requirements.</p> <p>In Your MainActivity to add</p> <pre><code>Button DB=(Button)findViewById(R.id.DB); DB.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Double price = Double.parseDouble(et2.getText().toString()); int amount = Integer.parseInt(et3.getText().toString()); MySQLiteHelper db = new MySQLiteHelper (MainActivity.this); db.createItem(new Item(et1.getText().toString(),price,amount)); db.close(); } }); </code></pre> <p>MySQLiteHelper</p> <pre><code>public class MySQLiteHelper extends SQLiteOpenHelper { public static final String TABLE_NAME = "caffe"; // private SQLiteDatabase database; // private MySQLiteHelper dbHelper; private String[] allColumns = {MySQLiteHelper.COLUMN_ARTIKAL,MySQLiteHelper.COLUMN_PRICE, MySQLiteHelper.COLUMN_AMOUNT}; public static final String COLUMN_ID = "_id"; public static final String COLUMN_ARTIKAL = "artikal"; public static final String COLUMN_PRICE = "price"; public static final String COLUMN_AMOUNT = "amount"; // public static final String COLUMN_TIME = "date&amp;time"; Context context; private static final String DATABASE_NAME = "caffe.db"; private static final int DATABASE_VERSION = 1; // changed the below private static final String DATABASE_CREATE = "create table " + TABLE_NAME + "(" + COLUMN_ID + " integer primary key autoincrement," + COLUMN_ARTIKAL + " text not null," + COLUMN_PRICE + " real not null," + COLUMN_AMOUNT + " integer not null)"; public MySQLiteHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); context= context; } @Override public void onCreate(SQLiteDatabase database) { database.execSQL(DATABASE_CREATE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.w(MySQLiteHelper.class.getName(), "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data"); db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); onCreate(db); } public void createItem(Item item) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(MySQLiteHelper.COLUMN_ARTIKAL, item.getArticle().toString()); values.put(MySQLiteHelper.COLUMN_PRICE, Double.toString(item.getPrice()).toString()); values.put(MySQLiteHelper.COLUMN_AMOUNT, Integer.toString(item.getAmount()).toString()); //Toast.makeText(context, "Inserted", 1000).show(); db.insert(MySQLiteHelper.TABLE_NAME, null,values); } public List&lt;Item&gt; getAllItems() { List&lt;Item&gt; items = new ArrayList&lt;Item&gt;(); SQLiteDatabase db = this.getWritableDatabase(); Cursor cursor = db.query(MySQLiteHelper.TABLE_NAME, allColumns, null, null, null, null, null); cursor.moveToFirst(); while (!cursor.isAfterLast()) { Item cursorItem = cursorToItem(cursor); items.add(cursorItem); cursor.moveToNext(); } cursor.close(); return items; } private Item cursorToItem(Cursor cursor) { Item item = new Item(); item.setArticle(cursor.getString(0)); item.setPrice(cursor.getDouble(1)); item.setAmount(cursor.getInt(2)); return item; } } </code></pre> <p>Item class</p> <pre><code>public class Item { String article; Double price; int amount; public Item() { } public Item(String article,Double price, int amount) { this.article=article; this.price=price; this.amount= amount; } public String getArticle() { return article; } public void setArticle(String article) { this.article = article; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public int getAmount() { return amount; } public void setAmount(int amount) { this.amount = amount; } </code></pre> <p>}</p> <p>Snap shot </p> <p><img src="https://i.stack.imgur.com/9YeRW.png" alt="enter image description here"></p> <p>Display on show button click. The toast displays only article name.</p> <pre><code> MySQLiteHelper db = new MySQLiteHelper (MainActivity.this); List&lt;Item&gt; contacts = db.getAllItems(); StringBuilder sb = new StringBuilder(); for (Item cn : contacts) { sb.append(cn.getArticle()); sb.append("\n"); } Toast.makeText(MainActivity.this,sb, 1000).show(); </code></pre> <p><img src="https://i.stack.imgur.com/fRJkB.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload