如何在用户插入标题后创建表格,然后在该表格中进行修改(添加,删除和更新行)?这是我不太确定的代码行:+ private String DATABASE_TABLE = ListTitle.getStringListTitle(); 我有一个Activity(ListTitle),其中包含EditText和Button的简单布局。我希望输入是SQLite数据库中表的名称。此活动指向另一个活动(NewList)。以下是活动的代码:
public class ListTitle extends AppCompatActivity {
private EditText inputListTitle;
private Button buttonNext;
private String stringListTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_title);
initUI();
setButtonNext();
}
private void initUI()
{
inputListTitle = (EditText) findViewById(R.id.input_list_title);
buttonNext = (Button) findViewById(R.id.button_next);
}
private void setButtonNext()
{
buttonNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!inputListTitle.getText().toString().equals(""))
{
ListTitle.this.stringListTitle = inputListTitle.getText().toString();
Intent intent = new Intent(ListTitle.this, NewList.class);
startActivity(intent);
}
else
{
Toast noInput = Toast.makeText(getApplicationContext(), "Sie müssen einen Titel für die Liste eintragen", Toast.LENGTH_SHORT);
noInput.show();
}
}
});
}
public String getStringListTitle()
{
return stringListTitle;
}
}
此活动(NewList)允许用户在他在上一个活动(ListTitle)中命名的表中进行修改。这是另一个活动的代码
public class NewList extends AppCompatActivity {
private WordPairDatabase wordPairDB;
private WordPair lastEntry;
private TextView titleLastEntry;
private EditText inputWord, inputEquivalent, lastWord, lastEquivalent;
private Button addButton, modifyButton, deleteButton;
private String stringInputWord, stringInputEquivalent, stringLastWord, stringLastEquivalent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_list);
initDatabase();
initUI();
setAddButton();
setLastEntryUI();
setDeleteButton();
setModifyButton();
}
private void initDatabase() {
wordPairDB = new WordPairDatabase(this);
wordPairDB.open();
}
public void initUI()
{
titleLastEntry = (TextView) findViewById(R.id.last_entry);
inputWord = (EditText) findViewById(R.id.input_word);
inputEquivalent = (EditText) findViewById(R.id.input_equivalent);
lastWord = (EditText) findViewById(R.id.last_word);
lastEquivalent= (EditText) findViewById(R.id.last_equivalent);
addButton = (Button) findViewById(R.id.button_add);
modifyButton= (Button) findViewById(R.id.button_modify);
deleteButton = (Button) findViewById(R.id.button_delete);
}
public void setAddButton()
{
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stringInputWord = inputWord.getText().toString();
stringInputEquivalent = inputEquivalent.getText().toString();
if (!stringInputWord.equals("") && !stringInputEquivalent.equals(""))
{
wordPairDB.addWordPair(new WordPair(stringInputWord, stringInputEquivalent));
setLastEntryUI();
inputWord.setText("");
inputEquivalent.setText("");
}
else
{
Toast noInput = Toast.makeText(getApplicationContext(), "Sie haben nicht alles eingetragen", Toast.LENGTH_SHORT);
noInput.show();
}
}
});
}
public void setLastEntryUI()
{
lastEntry = wordPairDB.retrieveLast();
if (lastEntry!=null)
{
titleLastEntry.setVisibility(View.VISIBLE);
lastWord.setVisibility(View.VISIBLE);
lastWord.setText(lastEntry.getWord());
lastEquivalent.setVisibility(View.VISIBLE);
lastEquivalent.setText(lastEntry.getEquivalent());
modifyButton.setVisibility(View.VISIBLE);
deleteButton.setVisibility(View.VISIBLE);
}
else
{
titleLastEntry.setVisibility(View.INVISIBLE);
lastWord.setVisibility(View.INVISIBLE);
lastEquivalent.setVisibility(View.INVISIBLE);
modifyButton.setVisibility(View.INVISIBLE);
deleteButton.setVisibility(View.INVISIBLE);
}
}
public void setDeleteButton()
{
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stringLastWord = lastWord.getText().toString();
stringLastEquivalent = lastEquivalent.getText().toString();
wordPairDB.deleteWordPair(new WordPair(stringLastWord, stringLastEquivalent));
setLastEntryUI();
}
});
}
private void setModifyButton()
{
modifyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stringLastWord = lastWord.getText().toString();
stringLastEquivalent = lastEquivalent.getText().toString();
wordPairDB.modifyWordPair(new WordPair(stringLastWord, stringLastEquivalent));
setLastEntryUI();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
wordPairDB.close();
}
}
这是我处理与数据库相关的所有内容的类
public class WordPairDatabase {
private static final int DATABASE_VERSION = 1;
private final String DATABASE_NAME = "wortlerner.db";
private final String COLUMN_ID = "_id";
private final String COLUMN_WORD = "word";
private final String COLUMN_EQUIVALENT = "equivalent";
private String DATABASE_TABLE = getLastCreatedTable();
private WordPairDBOpenHelper dbHelper;
private SQLiteDatabase db;
private Cursor cursor;
public WordPairDatabase(Context context)
{
dbHelper = new WordPairDBOpenHelper(context, DATABASE_NAME, null,DATABASE_VERSION);
}
public String getLastCreatedTable()
{
return lastCreatedTable;
};
public void open() throws SQLException {
try
{
db = dbHelper.getWritableDatabase();
}
catch (SQLException e)
{
db = dbHelper.getReadableDatabase();
}
}
public void close() {db.close();}
public void addWordPair(WordPair wordPair)
{
ContentValues values = new ContentValues();
values.put(COLUMN_WORD, wordPair.getWord());
values.put(COLUMN_EQUIVALENT, wordPair.getEquivalent());
db.insert(DATABASE_TABLE, null, values);
}
public void deleteWordPair(WordPair wordPair)
{
String toDelete = COLUMN_WORD + "=?";
String[] argument = new String[]{wordPair.getWord()};
db.delete(DATABASE_TABLE, toDelete, argument);
}
public WordPair retrieveLast()
{
String query = "SELECT * FROM "+ DATABASE_TABLE +" ORDER BY "+ COLUMN_ID +" DESC LIMIT 1";
cursor = db.rawQuery(query, null);
if (cursor.moveToLast())
{
WordPair result = new WordPair(cursor.getString(1), cursor.getString(2));
cursor.close();
return result;
}
else
{
return null;
}
}
public void modifyWordPair(WordPair wordPair)
{
String query = "SELECT * FROM "+ DATABASE_TABLE +" ORDER BY "+ COLUMN_ID +" DESC LIMIT 1";
cursor = db.rawQuery(query, null);
cursor.moveToLast();
int lastID = cursor.getInt(0);
ContentValues values = new ContentValues();
values.put(COLUMN_WORD, wordPair.getWord());
values.put(COLUMN_EQUIVALENT, wordPair.getEquivalent());
db.update(DATABASE_TABLE, values, COLUMN_ID+"="+lastID, null);
cursor.close();
}
private class WordPairDBOpenHelper extends SQLiteOpenHelper {
private final String DATABASE_CREATE = "create table "
+ DATABASE_TABLE + " (" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_WORD
+ " text not null, " + COLUMN_EQUIVALENT + " text not null);";
public WordPairDBOpenHelper(Context c, String dbname, SQLiteDatabase.CursorFactory factory, int version) {
super(c, dbname, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
lastCreatedTable = DATABASE_TABLE;
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
}
提前感谢您的帮助
答案 0 :(得分:0)
您可以在数据库助手类中使用类变量以及检索方法。
public class WordPairDatabase {
private static final int DATABASE_VERSION = 1;
private final String DATABASE_NAME = "wortlerner.db";
private final String COLUMN_ID = "_id";
private final String COLUMN_WORD = "word";
private final String COLUMN_EQUIVALENT = "equivalent";
private String DATABASE_TABLE = ListTitle.getStringListTitle();
private WordPairDBOpenHelper dbHelper;
private SQLiteDatabase db;
private Cursor cursor;
private String lastCreatedTable; //<<<<<<
public String getLastCreatedTable() {
return lastCreatedTable;
} ..........
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
lastCreatedTable = DATABASE_TABLE; //<<<<<<<<<<
}
然后使用以下内容检索表名
String the_tablename = WordPairDatabase.getLastCreatedTable();
这确实假设您只想创建最后一个,这就是我如何解释您的问题。
例如,你可以用两种方式使用它(可能太不灵活): -
public void deleteWordPair(WordPair wordPair)
{
String toDelete = COLUMN_WORD + "=?";
String[] argument = new String[]{wordPair.getWord()};
db.delete(lastCreatedTable, toDelete, argument); //<<<<<<
}
或者您可以使用: -
public void deleteWordPair(WordPair wordPair, tableName) //<<<<<<
{
String toDelete = COLUMN_WORD + "=?";
String[] argument = new String[]{wordPair.getWord()};
db.delete(tablename, toDelete, argument); //<<<<<<
}
以及: -
wordPairDB.deleteWordPair(new WordPair(stringLastWord, stringLastEquivalent),WordPairDatabase.getLastCreatedTable());
(在setDeleteButton
类的NewList
方法中使用)
注意我没有做过任何测试,因此可能会出现奇怪的错误。
我现在理解你的问题你想要的是从 EditText inputListTitle 获取用户输入并在 NewList 活动中使用它然后用它来创建一个数据库中的表。
通常,将数据传递给调用活动的可接受方式是通过Intent来实现,该Intent用于通过添加IntentExtras来调用活动。
这些使用相对简单,你使用Intent的'putExtra'方法,该方法有两个参数。 a)数据本身和b)识别数据的关键。
例如yourintent.putExtra("myIntentKey","mydata");
必须在创建Intent之后和调用startActivity方法之前完成。
您可以使用众多IntentExtras。
在启动/调用的活动中,然后使用密钥检索IntentExtras。
例如String mypassseddata = getIntent.getStringExtra(“myIntentKey”);
所以要实现这个: -
1)在ListTitle活动中更改(添加1行)
private EditText inputListTitle;
private Button buttonNext;
private String stringListTitle;
到
private EditText inputListTitle;
private Button buttonNext;
private String stringListTitle;
public String INTENTKEY_INPUTTITLE = "inputtitle"; //<<<<<<<<
2)再次在ListTitle活动中更改
ListTitle.this.stringListTitle = inputListTitle.getText().toString();
Intent intent = new Intent(ListTitle.this, NewList.class);
startActivity(intent);
到
ListTitle.this.stringListTitle = inputListTitle.getText().toString();
Intent intent = new Intent(ListTitle.this, NewList.class);
intent.putExtra(INTENTKEY_INPUTTITLE,inputListTitle.getText().toString);
startActivity(intent);
请注意! ListTitle.this.stringListTitle = inputListTitle.getText().toString();
是多余的,因此除非(假设您无意在stringListTitle
活动中的其他位置使用ListTitle
,否则您可以将其删除。此外,getStringListTitle
将是多余的,因此也可以删除,stringListTitle
变量的声明也可以删除。
因此,步骤1和2会将输入(标题)传递给NewList活动。
3)在NewList
活动的onCreate
方法中检索意图。
行后
setModifyButton();
添加
String passsedLastTitle = getIntent.getStringExtra(ListTitle.INTENTKEY_INPUTTITLE);
if (passedLastTitle.length() < 1) {
// handle empty string for example
finish(); // finish the activity and return to ListTitle
}
// String variable passedLastTitle = input from ListTitle
假设您希望将此值放入 WordPairDatabase 类中的 DATABASE_TABLE 变量,则: -
你应该在WordPairDatabase类中有一个方法,一个setter,例如。
public void setDatabaseTable(String newTableName) {
DATABASE_TABLE = newTableName;
}
然后,您可以从NewList
调用此内容,但是在创建WordPairDatabase实例之后才能调用此内容。
此外,如果您的目标是根据用户输入创建新表,我不相信这会发生。您似乎拥有创建表的代码的唯一位置是WordPairDBOpenHelper的onCreate
方法。
onCreate
方法仅在数据库文件的生命周期内自动运行一次。即它仅在创建数据库时运行。每次创建SQLiteOpenHelper子类的实例时都不会运行它。因此,您需要创建一个方法来创建新表。这样的方法会/应该将表名作为参数。
答案 1 :(得分:0)
在DBOpenHelper类中重写onConfigure方法并再次调用onCreate方法。喜欢这个
public void onConfigure(SQLiteDatabase db) {
super.onConfigure(db);
onCreate(db);
}
并且在onCreate方法中使用像这样的try catch
try {
final String DATABASE_CREATE = "create table "
+ DATABASE_TABLE + " (" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_WORD
+ " text not null, " + COLUMN_EQUIVALENT + " text not null);";
db.execSQL(DATABASE_CREATE);
}catch (Exception e){
Log.e("Found exception: ",e.toString());
}