我是android的新手。我试图在edittext中获取浮动值并将其存储在数据库中。然后我得到了那些存储值的总数。但我没有得到小数点后的值。 这是我的代码......
package com.example.usaukglu.tablayoyt;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Income extends AppCompatActivity implements View.OnClickListener {
TextView amount, payer, note, show;
EditText edi_amount, payer_name, edit_note;
Button save, cancel;
DatabaseHandler db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_income);
save = (Button) findViewById(R.id.save);
cancel = (Button) findViewById(R.id.cancel);
amount = (TextView) findViewById(R.id.amount);
payer = (TextView) findViewById(R.id.payer);
note = (TextView) findViewById(R.id.note);
show = (TextView) findViewById(R.id.show);
edi_amount = (EditText) findViewById(R.id.edit_amount);
payer_name = (EditText) findViewById(R.id.edit_payer);
edit_note = (EditText) findViewById(R.id.edit_note);
//date= (EditText) findViewById(R.id.date);
db = new DatabaseHandler(this);
save.setOnClickListener(this);
cancel.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.save) {
String amounts,payers,notes;
amounts=edi_amount.getText().toString();
payers= payer_name.getText().toString();
notes= edit_note.getText().toString();
if(amounts.isEmpty())
{
edi_amount.setError("Amounts should not be blank");
}
else if (payers.isEmpty()){
payer_name.setError("Payer name should not be blank");
}
else {
Double a =new Double(amounts);
DataProvider provider = new DataProvider(""+a, payers, notes);
db.addInformation(provider);
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
}
}
if (view.getId() == R.id.cancel) {
Intent i =new Intent(this,MainActivity.class);
startActivity(i);
}
}
public void show(String data) {
show.setText(data);
}
}
这是dataProvider类:
package com.example.usaukglu.tablayoyt;
public class DataProvider {
private String money;
private String name;
private String desc;
public DataProvider(String money, String name, String desc) {
this.money = money;
this.name = name;
this.desc = desc;
}
public String getMoney() {
return money;
}
public void setMoney(String money) {
this.money = money;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
DatabaseHandler类:
package com.example.usaukglu.tablayoyt;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import static com.example.usaukglu.tablayoyt.R.id.amount;
import static com.example.usaukglu.tablayoyt.R.id.note;
import static com.example.usaukglu.tablayoyt.R.id.payer;
public class DatabaseHandler extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "income.db";
public static final String TABLE_NAME = "income_table";
public static final String ID="id";
public static final String AMOUNT = "amount";
public static final String PAYER_NAME = "payer";
public static final String NOTE = "note";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String query = "CREATE TABLE " + TABLE_NAME +
"(" +ID+ " integer primary key autoincrement, " + AMOUNT + " real, " + PAYER_NAME + " text, " + NOTE + " text " + ")";
sqLiteDatabase.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(sqLiteDatabase);
}
public void addInformation(DataProvider provider){
SQLiteDatabase db = getWritableDatabase();
ContentValues values=new ContentValues();
values.put(AMOUNT,provider.getMoney());
values.put(PAYER_NAME,provider.getName());
values.put(NOTE,provider.getDesc());
db.insert(TABLE_NAME, null, values);
db.close();
}
public Cursor display()
{
SQLiteDatabase sqLiteDatabase=getReadableDatabase();
Cursor res= sqLiteDatabase.rawQuery("SELECT * FROM "+TABLE_NAME,null);
return res;
}
public double getTotalOfAmount() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("SELECT SUM(AMOUNT) FROM " + TABLE_NAME, null);
c.moveToFirst();
double i = c.getInt(0);
c.close();
return i;
}
}
FragmentIncome类我想要显示数据: package com.example.usaukglu.tablayoyt;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class FragmentIncome extends Fragment {
ListView list;
private DatabaseHandler handler;
private SQLiteDatabase database;
private List<DataProvider> amountList;
private ListDataAdaptar listDataAdaptar;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.fragment_income,container,false);
list=(ListView) view.findViewById(R.id.listView);
amountList=new ArrayList<>();
handler=new DatabaseHandler(getContext());
database=handler.getReadableDatabase();
Cursor cursor=handler.display();
if(cursor.moveToFirst()){
do {
String amount,payer,note;
amount= cursor.getString(cursor.getColumnIndex(handler.AMOUNT));
payer=cursor.getString(cursor.getColumnIndex(handler.PAYER_NAME));
note=cursor.getString(cursor.getColumnIndex(handler.NOTE));
DataProvider provider=new DataProvider(amount,payer,note);
amountList.add(provider);
listDataAdaptar=new ListDataAdaptar(getContext(),R.layout.display_income_row,amountList);
list.setAdapter(listDataAdaptar);
}while (cursor.moveToNext());
}
return view;
}
}
答案 0 :(得分:0)
您正在数据库中使用String变量。在表中使用float数据类型来运行SUM查询。
您的DataProvider类应该是这样的 -
public class DataProvider {
private Float money;
private String name;
private String desc;
public DataProvider(Float money, String name, String desc) {
this.money = money;
this.name = name;
this.desc = desc;
}
public Float getMoney() {
return money;
}
public void setMoney(Float money) {
this.money = money;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}