-3
有一個奇怪的問題我花了一些時間在我輸入消息到聊天應用程序的位置。消息進入arrayList以及數據庫。當我離開應用程序的聊天部分時,我希望能夠返回並將數據庫中現有的消息插入到arrayList中,以便它們可見。Java - Android Studio - 數據庫返回列名稱,而不是列中的消息
當我重新進入應用程序的聊天部分時,我得到的只是「KEY_MESSAGE」而不是實際輸入的消息。
任何想法?
感謝
public class ChatWindow extends AppCompatActivity {
ListView listView;
Button sendButton;
ArrayList<String> arrayList = new ArrayList<String>(); // arrayList to hold "chat"
ChatDatabaseHelper Cdb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_window);
listView = (ListView)findViewById(R.id.chatListView);
final EditText editText = (EditText)findViewById(R.id.messageText);
sendButton = (Button)findViewById(R.id.sendButton);
final ChatAdapter messageAdapter = new ChatAdapter(this);
listView.setAdapter(messageAdapter);
Cdb = new ChatDatabaseHelper(this);
Cursor cursor = Cdb.getMessages();
while (cursor.moveToNext()) {
arrayList.add(cursor.getString(cursor.getColumnIndex(Cdb.KEY_MESSAGE)));
}
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String chatText = editText.getText().toString(); // changing editText to String
arrayList.add(chatText); // adding the string from EditTest to arrayList
boolean isInserted = Cdb.insertData(chatText); // inserting the message text into the database
if(isInserted = true)
Toast.makeText(ChatWindow.this,"Message Sent",Toast.LENGTH_SHORT).show();
else
Toast.makeText(ChatWindow.this,"Message not Sent",Toast.LENGTH_SHORT).show();
messageAdapter.notifyDataSetChanged();
editText.setText(" ");
}
});
}
// for loop class.
class ChatAdapter extends ArrayAdapter<String>{ // custom adapter class // when youc all the adapter it forms the for loop for you.
public ChatAdapter(Context ctx) {
super(ctx, 0);
} // default constructor
// method to return the number of rows that will be in your array
public int getCount(){ // will tell how many times to run a for loop
return arrayList.size(); //will return the size of the array
}
public String getItem(int position){
return arrayList.get(position); // will return the item at position
}
// getview method
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater Inflater = ChatWindow.this.getLayoutInflater(); // an inflater inflates the xml layout into a view.
View result = null;
if(position%2 == 0){ // if position number in the array is odd do this, if number is even, do this.
result = Inflater.inflate(R.layout.chat_row_incoming, null); // depending on the position, show layout incoming
} else {
result = Inflater.inflate(R.layout.chat_row_outgoing,null); // depending on the position, show layout outgoing
}
TextView message = (TextView)result.findViewById(R.id.messageText); // creating a message of type TextView connected to messageText
message.setText(getItem(position)); // the messageText is the input typed in.
return result; // return the view which is the Inflater.
}
} // end of chat adapter class
@Override
protected void onDestroy(){
super.onDestroy();
Cdb.close();
String ACTIVITY_NAME = "ChatWindow";
Log.i(ACTIVITY_NAME,"In onDestroy()");
}
}
這是我的數據庫幫助類:
public class ChatDatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Chats.db";
public static final String TABLE_NAME = "chatMessages";
public static final int dbVersion = 1;
public static final int KEY_ID = 0;
public static final String KEY_MESSAGE = "MESSAGE";
public ChatDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, dbVersion);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " +TABLE_NAME+ "(ID INTEGER PRIMARY KEY AUTOINCREMENT,"+KEY_MESSAGE+" text)");
Log.i("ChatDataBaseHelper","Calling OnCreate");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
Log.i("ChatDataBaseHelper","Calling OnUpgrade, oldVersion=" + oldVersion + "newVersion" + newVersion);
}
public boolean insertData(String message){
SQLiteDatabase db = this.getWritableDatabase(); // creating the database
ContentValues contentValues = new ContentValues();
try {
contentValues.put(KEY_MESSAGE, message);
db.insert(TABLE_NAME, null, contentValues);
return true;
}catch(Exception e){
return false;
}
}
public Cursor getMessages(){
SQLiteDatabase db = this.getWritableDatabase(); // creating the database
Cursor msg = db.rawQuery("select * from " + TABLE_NAME,null);
return msg;
}
}
不幸的是,這並沒有解決它對我來說。我結束了從應用程序清除數據,再次調試,它工作正常。該應用程序保持了一些「舊」數據庫不能正常工作。 儘管謝謝你的回覆! –