我能够在主要活动中将数据写入数据库,但是我在阅读和显示不同活动中的数据时遇到了问题。我正在使用带有' addData'的SQLHelperDatabase类。方法(根据调试器工作正常)和showData方法(这就是问题所在)。在主活动中输入数据后,我希望能够按下发送消息按钮和要在新活动上显示的数据。我已经坚持了一个星期了,我一直在搜索WWW,每个教程都说我的代码应该可行。请有人帮我解决这个令人沮丧的考验。
package com.doors.waynderful.myapp;
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class DisplayMessageActivity extends ActionBarActivity {
DatabaseHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db = new DatabaseHelper(this);
Cursor cursor = db.showData();
cursor.moveToFirst();
String nam = cursor.getString(cursor.getColumnIndex(DatabaseHelper.THE_TABLE_COLUMN_NAME));
if (!cursor.isClosed()){
cursor.close();
}
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(nam);
setContentView(textView);
//setContentView(R.layout.activity_display_message);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_display_message, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
以下是SQLHelperDatabase。
package com.doors.waynderful.myapp;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper
{
public static final String THE_DATABASE = "theDatabase";
public static final String THE_TABLE = "infoTable";
public static final String THE_TABLE_COLUMN_ID = "id";
public static final String THE_TABLE_COLUMN_NAME = "name";
public static final String THE_TABLE_COLUMN_DETAILS = "details";
DatabaseHelper(Context context)
{
super(context,THE_DATABASE, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " +THE_TABLE+"(id integer primary key, name text, details text) ");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists " + THE_TABLE + "");
onCreate(db);
}
public void addData(String nameIn, String detailsIn)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues data = new ContentValues();
data.put("name", nameIn);
data.put("details", detailsIn);
db.insert(THE_TABLE,null,data);
}
public Cursor showData()
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor;
cursor = db.rawQuery("select * from "+THE_TABLE+"", null);
return cursor;
}
}
以下是MainActivity
package com.doors.waynderful.myapp;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.doors.waynderful.myapp.MESSAGE";
DatabaseHelper myDb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void sendMessage(View view){
myDb = new DatabaseHelper(this);
Intent intent = new Intent(this,DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_name);
EditText editText1 = (EditText) findViewById(R.id.edit_details);
String name = editText.getText().toString();
String details = editText1.getText().toString();
myDb.addData(name, details);
//intent.putExtra(EXTRA_MESSAGE,message);
startActivity(intent);
}
}
LOGCAT显示
03-31 09:40:31.100 1351-1351 / com.doors.waynderful.myapp E / SQLiteLog:(1)没有这样的表:infoTable
答案 0 :(得分:1)
我不确定为什么你认为你必须在数据库中有一行id = 1
如果要查找具有特定名称的行,请使用sql语句选择具有该名称的行。
如果要列出所有行,请选择所有行。
但是插入一行,然后假设你知道它的id就不会起作用了。
此外,您应该使用使用布局的(当前已注释掉的)代码,而不是创建新的文本视图并将其设置为内容视图。
最后,您似乎没有任何实际在数据库中创建表的代码?