подскажите что я упустил...
Код: Выделить всё
package com.tsalger.project;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class InputActivity extends Activity implements OnClickListener {
TextView tvO, tvC, tvNH, tvCH;
EditText etO, etC, etNH, etCH;
Button btnSave, btnList;
DBHelper dbHelper;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.input);
tvO = (TextView) findViewById(R.id.tvO);
tvC = (TextView) findViewById(R.id.tvC);
tvNH = (TextView) findViewById(R.id.tvNH);
tvCH = (TextView) findViewById(R.id.tvCH);
etO = (EditText) findViewById(R.id.etO);
etC = (EditText) findViewById(R.id.etC);
etNH = (EditText) findViewById(R.id.etNH);
etCH = (EditText) findViewById(R.id.etCH);
btnSave = (Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener(this);
btnList = (Button) findViewById(R.id.btnList);
btnList.setOnClickListener(this);
dbHelper = new DBHelper(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// создаем объект для данных
ContentValues cv = new ContentValues();
// получаем данные из полей ввода
String operation = etO.getText().toString();
String completed = etC.getText().toString();
String norm = etNH.getText().toString();
String cost = etCH.getText().toString();
double c = 0;
double nh = 0;
double ch = 0;
// Проверяем поля на пустоту
if (TextUtils.isEmpty(etC.getText().toString())
|| TextUtils.isEmpty(etNH.getText().toString())
|| TextUtils.isEmpty(etCH.getText().toString())) {
return;
}
// читаем EditText и заполняем переменные числами
c = Float.parseFloat(etC.getText().toString());
nh = Float.parseFloat(etNH.getText().toString());
ch = Float.parseFloat(etCH.getText().toString());
double r = (c/nh) * ch;
double salary = r;
// подключаемся к БД
SQLiteDatabase db = dbHelper.getWritableDatabase();
switch (v.getId()) {
case R.id.btnSave:
// подготовим данные для вставки в виде пар: наименование столбца - значение
cv.put("operation", operation);
cv.put("completed", completed);
cv.put("norm", norm);
cv.put("cost", cost);
cv.put("salary", salary);
// вставляем запись и получаем ее ID
long rowID = db.insert("mytable", null, cv);
break;
case R.id.btnList:
// делаем запрос всех данных из таблицы mytable, получаем Cursor
Cursor cur = db.query("mytable", null, null, null, null, null, null);
// ставим позицию курсора на первую строку выборки
// если в выборке нет строк, вернется false
if (cur.moveToFirst()) {
// определяем номера столбцов по имени в выборке
int idColIndex = cur.getColumnIndex("id");
int operationColIndex = cur.getColumnIndex("Operation");
int completedColIndex = cur.getColumnIndex("Completed");
int normColIndex = cur.getColumnIndex("Norm");
int costColIndex = cur.getColumnIndex("Cost");
int salaryColIndex = cur.getColumnIndex("Salary");
break;
}
dbHelper.close();
}
class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
// конструктор суперкласса
super(context, "myDB", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
// создаем таблицу с полями
db.execSQL("create table mytable ("
+ "id integer primary key autoincrement,"
+ "operation text,"
+ "completed text"
+ "norm"
+ "cost"
+ "salary"+ ");");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
}
}
