我一直在學習如何在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" />
爲什麼在插入查詢中不插入總值? –
不會在InsertProduct()插入總值,允許用戶添加總值。應用程序需要計算總值。 我也嘗試過最初但它導致應用程序崩潰 –
其實問題是你插入字符串值在** insert()**方法,而不是double值,這是創建表中的列的類型。在創建,插入,選擇,更新等所有查詢中使用相同類型是件好事。希望這會幫助你。 –