我目前的代码存在问题。我在android工作。这是我的学校实验室之一,但我不知道为什么它不起作用。它与我在上一个实验室中创建的聊天窗口的数据库有关。我想将所有聊天内容存储到数据库中。我一直很难在编程中使用数据库。我发布了下面的两个课程。如果有人能提供帮助,我们将非常感激。
public class ChatWindow extends Activity {
Button send;
ListView chat;
EditText message;
ArrayList<String> list;
String addText;
ChatAdapter messageAdapter;
public static final String ACTIVITY_NAME = "ChatWindow";
String returnedMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_window);
send = (Button) findViewById(R.id.send_button);
chat = (ListView) findViewById(listView);
message = (EditText) findViewById(R.id.message_text);
list = new ArrayList<>();
messageAdapter = new ChatAdapter( this );
chat.setAdapter(messageAdapter);
ChatDatabaseHelper chatDH = new ChatDatabaseHelper(ChatWindow.this.getApplicationContext());
final SQLiteDatabase myDatabase = chatDH.getWritableDatabase();
Cursor cursor = myDatabase.query(false, MY_TABLE, columns, null, null, null, null, null, null);
int numOfResults = cursor.getCount();
int numOfColumns = cursor.getColumnCount();
int getMessageIndex = cursor.getColumnIndex(KEY_MESSAGE);
int array[] = new int[] {R.id.message_text};
SimpleCursorAdapter adptr = new SimpleCursorAdapter(this, R.layout.activity_chat_window, cursor, new String[] {KEY_ID, KEY_MESSAGE}, array, 0 );
chat.setAdapter(adptr);
for(int i = 0; i < numOfResults; i++){
returnedMessage = cursor.getString(getMessageIndex);
cursor.moveToNext();
}
cursor.moveToFirst();
while(!cursor.isAfterLast()){
returnedMessage = cursor.getString(getMessageIndex);
cursor.moveToNext();
}
cursor.close();
send.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
addText = message.getText().toString();
list.add(addText);
messageAdapter.notifyDataSetChanged();
message.setText("");
ContentValues newValue = new ContentValues();
newValue.put(KEY_MESSAGE, addText);
myDatabase.insert(MY_TABLE, "", newValue);
}
});
}
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);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = ChatWindow.this.getLayoutInflater();
View result;
if(position%2 == 0)
result = inflater.inflate(R.layout.chat_row_incoming, null);
else
result = inflater.inflate(R.layout.chat_row_outgoing, null);
TextView message = (TextView)result.findViewById(R.id.message_text);
message.setText( getItem(position) );
return result;
}
}
}
public class ChatDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "Messages.db";
private static final int VERSION_NUM = 1;
public static final String KEY_ID = "ID";
public static final String KEY_MESSAGE = "Message";
public static final String MY_TABLE = "MyTable";
public static final String [] columns = new String[] {KEY_ID, KEY_MESSAGE};
public ChatDatabaseHelper(Context ctx) {
super(ctx, DATABASE_NAME, null, VERSION_NUM);
}
public void onCreate(SQLiteDatabase db) {
String CREAT_TABLE = "CREATE TABLE " + MY_TABLE + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_MESSAGE + " TEXT " + ");";
db.execSQL(CREAT_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + MY_TABLE);
onCreate(db);
}
}