我正在尝试保存聊天记录以便稍后使用。我通过按钮单击保存聊天记录,我可以通过单击同一活动中的另一个按钮来正确读取它。但是当我关闭活动或关闭应用程序然后打开必须显示聊天记录的活动时,根本就没有数据。当我再次写作并尝试阅读历史记录时,它只会返回新的聊天记录。 这是我定义数据库的代码:
public class ChatDatabaseHelp extends SQLiteOpenHelper {
static String DATABASE_NAME;
static int VERSION_NUM=1;
public static String KEY_ID="key_id",KEY_MESSAGE="message";
public static String TABLE_NAME="messages";
public ChatDatabaseHelp(Context ctx)
{
super(ctx,DATABASE_NAME,null,VERSION_NUM);
}
@Override
public void onCreate(SQLiteDatabase db) {
String SQL="CREATE TABLE messages(key_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,message TEXT NOT NULL)";
db.execSQL(SQL);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
这是我读写聊天的地方:
public class ChatWindow extends AppCompatActivity {
EditText t_send;
Button send;
ChatAdapter messageAdapter;
ListView listView;
ChatDatabaseHelp helper;
private SQLiteDatabase db;
ArrayList<String> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_window);
t_send=(EditText)findViewById(R.id.editText);
list= new ArrayList<String>();
send=(Button)findViewById(R.id.button3);
listView=(ListView)findViewById(R.id.listview);
messageAdapter=new ChatAdapter(this);
listView.setAdapter(messageAdapter);
helper=new ChatDatabaseHelp(this);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
db=helper.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(helper.KEY_MESSAGE,t_send.getText().toString());
db.insert(helper.TABLE_NAME,null,cv);
list.add(t_send.getText().toString());
t_send.setText("");
messageAdapter.notifyDataSetChanged();
}
});
}
public void onfff(View view) {
db=helper.getReadableDatabase();
Cursor cursor=db.rawQuery("select * from messages " , null);
System.out.println("hiii3ii"+cursor.getCount());
while ((cursor.moveToNext())) {
list.add(cursor.getString(cursor.getColumnIndex("message")));
messageAdapter.notifyDataSetChanged();
}
}
private class ChatAdapter extends ArrayAdapter<String> {
public ChatAdapter(Context ctx){
super(ctx,0);
}
public int getCount()
{
return list.size();
}
public String getItem(int position)
{
return list.get(position);
}
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater=ChatWindow.this.getLayoutInflater();
View result=null;
if(position%2==0){
result=inflater.inflate(R.layout.chat_row_ingoing,null);
TextView message=(TextView)result.findViewById(R.id.message_text_in);
message.setText(getItem(position));}
else{
result=inflater.inflate(R.layout.chat_row_outgoing,null);
TextView message=(TextView)result.findViewById(R.id.message_text_out);
message.setText(getItem(position));}
return result;
}
}
@Override
protected void onDestroy() {
db.close();
super.onDestroy();
}
}
答案 0 :(得分:1)
我认为你在onCreate方法中调用了chatWindow类中的read database方法..
onfff() //above define this method call in onCreate()