2017-04-18 25 views
0

我一直在学习如何在Android Studio中使用SQL,通过学习在线模板和教程,但没有教程展示如何乘两列并添加计算的数据进入一个新的专栏。在SQLite中乘以两列并将其显示在Android Studio中textview

任务是乘以成本和数量得到总和。 现在,我已经设法了解如SQLite中的添加,更新和删除数据等基础知识。

我的继承人更新当前DBHelper.java

package ntws.itemsqlite; 

/** 
* Created by eddie on 13/4/2017. 
*/ 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.DatabaseUtils; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

import java.util.ArrayList; 
import java.util.HashMap; 

public class DBHelper extends SQLiteOpenHelper { 

    public static final String DATABASE_NAME = "ProductList.db"; 
    public static final String PRODUCTS_TABLE_NAME = "product"; 
    public static final String PRODUCTS_COLUMN_ID = "id"; 
    public static final String PRODUCTS_COLUMN_NAME = "name"; 
    public static final String PRODUCTS_COLUMN_COST = "cost"; 
    public static final String PRODUCTS_COLUMN_QUANTITY = "quantity"; 
    public static final String PRODUCTS_COLUMN_TOTAL = "total"; 

    private HashMap hp; 

    public DBHelper(Context context) { 
     super(context, DATABASE_NAME , null, 1); 
    } 



    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 
     db.execSQL(
       "create table products " + 
         "(id integer primary key, name text,cost double, quantity double, total double)" 
     ); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 
     db.execSQL("DROP TABLE IF EXISTS products"); 
     onCreate(db); 
    } 
public int getAllTotal(int cost){ 
    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor res = db.rawQuery("SELECT SUM(cost) FROM products", null); 
    if(res.moveToFirst()) 
    { 
     return res.getInt(0); 
    } 

    return cost; 
} 

    public boolean insertProduct (String name, String cost, String quantity) { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues contentValues = new ContentValues(); 
     contentValues.put("name", name); 

     contentValues.put("cost", cost); 
     contentValues.put("quantity", quantity); 

     db.insert("products", null, contentValues); 
     return true; 
    } 

    public Cursor getData(int id) { 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor res = db.rawQuery("select * from products where id="+id+" ", null); 

     return res; 
    } 

    public int numberOfRows(){ 
     SQLiteDatabase db = this.getReadableDatabase(); 
     int numRows = (int) DatabaseUtils.queryNumEntries(db, PRODUCTS_TABLE_NAME); 
     return numRows; 
    } 

    public boolean updateProduct (Integer id, String name, String cost, String quantity) { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues contentValues = new ContentValues(); 
     contentValues.put("name", name); 

     contentValues.put("cost", cost); 
     contentValues.put("quantity", quantity); 



     db.update("products", contentValues, "id = ? ", new String[] { Integer.toString(id) }); 
     return true; 
    } 

    public Integer deleteProduct (Integer id) { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     return db.delete("products", 
       "id = ? ", 
       new String[] { Integer.toString(id) }); 
    } 

    public ArrayList<String> getAllProduct() { 
     ArrayList<String> array_list = new ArrayList<String>(); 

     //hp = new HashMap(); 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor res = db.rawQuery("select * from products", null); 
     res.moveToFirst(); 

     while(res.isAfterLast() == false){ 
      array_list.add(res.getString(res.getColumnIndex(PRODUCTS_COLUMN_NAME))); 
      res.moveToNext(); 
     } 
     return array_list; 
    } 

    public double getItemTotal(int value) 
    { 
     String countQuery = "SELECT * FROM product"; 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor res = db.rawQuery(countQuery , null); 

     double itemtotal = 0; 
     if(res.moveToFirst()){ 
      String costStr = res.getString(res.getColumnIndex("cost")); 
      String qntStr = res.getString(res.getColumnIndex("quantity")); 

      itemtotal = Double.parseDouble(costStr) * Double.parseDouble(qntStr); 
      res.close(); 
     } 
     return itemtotal; 
    } 

    //end of DBHelper 

} 

正如你所看到的,我已经试过的方法调用getItemTotal我DBHelper.java增加。 目前,所有其它功能工作,我能够通过穿过DisplayProduct.java

我们替换相应的DATAS在activity_display_product.xml的TextViews是实现getItemTotal()到DisplayProduct.Java这样的问题方法功能可以运行计算然后分别更新。

DisplayProduct.Java

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_display_product); 
    name = (TextView) findViewById(R.id.editTextName); 

    cost = (TextView) findViewById(R.id.editTextCost); 
    quantity = (TextView) findViewById(R.id.editTextQuantity); 



    mydb = new DBHelper(this); 



    Bundle extras = getIntent().getExtras(); 
    if(extras !=null) { 
     int Value = extras.getInt("id"); 

     if(Value>0){ 
      //means this is the view part not the add PRODUCT part. 
      Cursor rs = mydb.getData(Value); 
      id_To_Update = Value; 
      rs.moveToFirst(); 

      String nam = 
rs.getString(rs.getColumnIndex(DBHelper.PRODUCTS_COLUMN_NAME)); 

      String cos = 
rs.getString(rs.getColumnIndex(DBHelper.PRODUCTS_COLUMN_COST)); 
      String quantit = 
rs.getString(rs.getColumnIndex(DBHelper.PRODUCTS_COLUMN_QUANTITY)); 


      if (!rs.isClosed()) { 
       rs.close(); 
      } 
      Button b = (Button)findViewById(R.id.button1); 
      b.setVisibility(View.INVISIBLE); 

      name.setText((CharSequence)nam); 
      name.setFocusable(false); 
      name.setClickable(false); 


      cost.setText((CharSequence)cos); 
      cost.setFocusable(false); 
      cost.setClickable(false); 

      quantity.setText((CharSequence)quantit); 
      quantity.setFocusable(false); 
      quantity.setClickable(false); 


     } 
    } 

总价值应该更换的TextView在activity_display_product.xml

更新

我知道我应该从DBHelper添加getItemTotal方法到DisplayProduct.java的onCreate,但我不知道如何实现。 所有示例都显示数组或列表视图。

有没有更好的方法,以便textview“editTextTotal”获取itemTotal?

如果有帮助, activity_display_product.xml的.xml文件如下所示。 除editTextTotal之外的所有内容都已正确分配。

activity_display_product.xml

<EditText 
      android:id="@+id/editTextTotal" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/editTextQuantity" 
      android:layout_marginTop="22dp" 
      android:layout_toEndOf="@+id/textView7" 
      android:ems="10" 
      android:inputType="text|number" /> 
+0

为什么在插入查询中不插入总值? –

+0

不会在InsertProduct()插入总值,允许用户添加总值。应用程序需要计算总值。 我也尝试过最初但它导致应用程序崩溃 –

+0

其实问题是你插入字符串值在** insert()**方法,而不是double值,这是创建表中的列的类型。在创建,插入,选择,更新等所有查询中使用相同类型是件好事。希望这会帮助你。 –

回答

0

问题是听到

cost text, quantity text, total text 

已添加文本,首先解析成这串翻番然后计算你的渴望 计算。

+0

没有方法getItemTotal()将文本包装成双? –

相关问题