我对 Android Studio 还是很陌生,我已经在不同的论坛中查看过这个问题,但我还没有完全找到一个与我的问题特别相关的问题(或者我至少没有足够的经验来识别它。)
我创建了一个活动,其中有人将 3 个不同的单词输入到 3 个不同的 EditText 视图中,并且应该在创建时将这 3 个单独的值插入到数据库的“第 1 列”或“COL_1”中,当然还会创建另一个意图并移动在检查/证明值已插入数据库后进行下一个活动。
我的问题是只有第三个单词作为一行(或记录)输入到数据库中,而不是前两个。对不起,就像我说我是新手,我错过了什么?:
编辑文本活动
package com.example.jonathan.om11;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class ContributionsActivity extends AppCompatActivity {
Button goToMainPageBtn;
ContribDatabase contribDatabase;
EditText contribution1,contribution2,contribution3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contributions);
contribDatabase = new ContribDatabase(this);
contribution1 = (EditText) findViewById(R.id.contribution1);
contribution2 = (EditText) findViewById(R.id.contribution2);
contribution3 = (EditText) findViewById(R.id.contribution3);
goToMainPageBtn = (Button) findViewById(R.id.goToMainPageBtn);
goToMainPageBtn.setOnClickListener(new View.OnClickListener(){
public void onClick (View v){
boolean isInserted = contribDatabase.insertData(
contribution1.getText().toString(),
contribution2.getText().toString(),
contribution3.getText().toString());
Toast.makeText(ContributionsActivity.this, "Thank you for contributing, enjoy!", Toast.LENGTH_SHORT).show();
if(isInserted == true) {
toContribPage(v);
} else {
Toast.makeText(ContributionsActivity.this, "Oops! Contributions did not save, try again please!", Toast.LENGTH_SHORT).show();
}
}
});
}
public void toContribPage(View v) {
Intent i = new Intent("android.intent.action.TabbedView");
startActivity(i);
}
}
SQLITEOPENHELPER 类
package com.example.jonathan.om11;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by Jonathan on 2/10/18.
*/
public class ContribDatabase extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Contributions.db";
public static final String TABLE_NAME = "Contributions_table";
// public static final String COL_1 = "ID";
public static final String COL_1 = "CONTRIBUTIONS";
public ContribDatabase(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
// db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT, CONTRIBUTIONS TEXT)");
db.execSQL("create table " + TABLE_NAME +" (CONTRIBUTIONS TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean insertData(String contribution1, String contribution2, String contribution3) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1,contribution1);
contentValues.put(COL_1,contribution2);
contentValues.put(COL_1, contribution3);
//Check for if data is inserted or not
long result = db.insert(TABLE_NAME,null,contentValues);
if(result == -1) {
return false;
} else {
return true;
}
}
// public Cursor getAllData() {
// SQLiteDatabase db = this.getWritableDatabase();
// Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
// return res;
// }
}