我想在编辑文本字段中输入日期,该日期应通过单击保存按钮存储在表格中。之后选择应该在第二页显示的数据怎么做。请帮助我。
这是我的代码
数据库Helper.java :
public class DatabaseHelper extends SQLiteOpenHelper
{
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Logcat tag
private static final String LOG = "DatabaseHelper";
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "Motolifedatabase";
// Table Names
private static final String TABLE_FLUIDHISTORY = "fluidhistory";
//fluid history-column names
private static final String FLUIDCHANGE_ID = "fluidchageid";
private static final String FCVEHICLE_ID = "vehicalid";
private static final String FCFLUID_ID = "fluidid";
private static final String LAST_CHANGE = "lastchangedate";
private static final String NEXT_CHANGE = "nextchangedate";
private static final String LAST_CHANGEODO = "lastchangeodo";
private static final String NEXT_CHANGEODO = "nextchangeodo";
// Table Create Statements
//fluidchange table stament
private static final String CREATE_TABLE_FLUIDCHANGE = "CREATE TABLE " + TABLE_FLUIDHISTORY
+ "(" + FLUIDCHANGE_ID + " INTEGER PRIMARY KEY," + FCVEHICLE_ID + " INTEGER"+ FCFLUID_ID + "INTEGER" + "DATE" +NEXT_CHANGE +"DATE"+LAST_CHANGEODO +"INTEGER" + NEXT_CHANGEODO +"INTEGER" +");";
@Override
public void onCreate(SQLiteDatabase db) {
// creating required tables
db.execSQL(CREATE_TABLE_FLUIDCHANGE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// on upgrade drop older tables
db.execSQL("DROP TABLE IF EXISTS " + TABLE_FLUIDHISTORY);
// create new tables
onCreate(db);
}
enter code here
public void addFluidhistory(Fluidhistory fluidhistory) {
// TODO Auto-generated method stub
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(LAST_CHANGE, fluidhistory.getLastchangedate()); // First Name
values.put(NEXT_CHANGE, fluidhistory.getNextchangedate());
values.put(LAST_CHANGEODO, fluidhistory.getLastchangeodo());
values.put(NEXT_CHANGEODO, fluidhistory.getNextchangeodo());// Last Name
// Inserting Row
db.insert(TABLE_FLUIDHISTORY , null, values);
// Closing database connection
db.close();
}
public List<Fluidhistory> getFluidHistory() {
// TODO Auto-generated method stub
List<Fluidhistory> nameVoList = new ArrayList<Fluidhistory>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_FLUIDHISTORY;
SQLiteDatabase db1 = this.getWritableDatabase();
Cursor cursor = db1.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Fluidhistory nameVO = new Fluidhistory();
nameVO.setLastchangedate(cursor.getString(0));
nameVO.setNextchangedate(cursor.getString(1));
nameVO.setLastchangeodo(cursor.getString(2));
nameVO.setNextchangeodo(cursor.getString(3));
// Adding NameVO to list
nameVoList.add(nameVO);
}
while (cursor.moveToNext());
}
// return NameVO list
return nameVoList;
}
}
main activity class:
她是主要的活动类代码,所以请参考此代码并帮助我如何更改它。
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private EditText edt1,edt2,edt3,edt4;
private DatabaseHelper handler;
@SuppressWarnings("unused")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_screen);
// StringBuffer details=new StringBuffer();
TextView names=(TextView)findViewById(R.id.textview1);
final EditText edt1=(EditText)findViewById(R.id.editText1);
final EditText edt2=(EditText)findViewById(R.id.editText2);
final EditText edt3=(EditText)findViewById(R.id.editText3);
final EditText edt4=(EditText)findViewById(R.id.editText4);
Button btn=(Button)findViewById(R.id.save);
final DatabaseHelper handler=new DatabaseHelper(this);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
handler.addFluidhistory(new Fluidhistory(edt1.getText().toString(),edt2.getText().toString(),edt3.getText().toString(),edt4.getText().toString()));
}
});
}
}
它不能为我工作。在输入值点击保存按钮后,应用程序将关闭。为什么我发生这样的事你能有人知道请帮助我。提前谢谢你。
更新logcat:
06-03 02:16:44.695: E/AndroidRuntime(1102): FATAL EXCEPTION: main
06-03 02:16:44.695: E/AndroidRuntime(1102): Process: com.example.tejuraju, PID: 1102
06-03 02:16:44.695: E/AndroidRuntime(1102): java.lang.NullPointerException
06-03 02:16:44.695: E/AndroidRuntime(1102): at com.example.tejuraju.MainActivity.onClick(MainActivity.java:57)
06-03 02:16:44.695: E/AndroidRuntime(1102): at android.view.View.performClick(View.java:4438)
06-03 02:16:44.695: E/AndroidRuntime(1102): at android.view.View$PerformClick.run(View.java:18422)
06-03 02:16:44.695: E/AndroidRuntime(1102): at android.os.Handler.handleCallback(Handler.java:733)
06-03 02:16:44.695: E/AndroidRuntime(1102): at android.os.Handler.dispatchMessage(Handler.java:95)
06-03 02:16:44.695: E/AndroidRuntime(1102): at android.os.Looper.loop(Looper.java:136)
06-03 02:16:44.695: E/AndroidRuntime(1102): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-03 02:16:44.695: E/AndroidRuntime(1102): at java.lang.reflect.Method.invokeNative(Native Method)
06-03 02:16:44.695: E/AndroidRuntime(1102): at java.lang.reflect.Method.invoke(Method.java:515)
06-03 02:16:44.695: E/AndroidRuntime(1102): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-03 02:16:44.695: E/AndroidRuntime(1102): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-03 02:16:44.695: E/AndroidRuntime(1102): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
为了保存数据,有很多选择。
如果它只是一个日期值,您可以使用共享首选项。
参考: http://www.vogella.com/tutorials/AndroidFileBasedPersistence/article.html
如果它将成为下一个活动中只需要的临时值,那么您可以使用通过INTENT传递值。
参考: http://www.vogella.com/tutorials/AndroidIntent/article.html
如果要将值保存在DB(Advisable for multiple records / table structure)中,可以使用SQL Lite
<强>参考:强> http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
答案 1 :(得分:0)
您已经为private DatabaseHelper handler;
所以在onCreate()
上不需要再次声明它。只需初始化,
handler=new DatabaseHelper(this);
您也不需要为EditText
声明变量。他们必须
edt1=(EditText)findViewById(R.id.editText1);
edt2=(EditText)findViewById(R.id.editText2);
edt3=(EditText)findViewById(R.id.editText3);
edt4=(EditText)findViewById(R.id.editText4);
还必须检查所有UI
的ID是否必须更正,并在first_screen.xml
文件中。