2016-10-11 48 views
-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; 

    } 
} 

回答

0

相反Cursor msg = db.rawQuery("select * from " + TABLE_NAME,null),你必须使用类似的东西寿这样的:

public String createDBandReturnValue(){ 
    SQLiteDatabase banco = context.openOrCreateDatabase("DATABASE_NAME", DATABASE_ACESS, null);//DATABASE_ACESS=0,1 or 2 
    Cursor cursor = banco.query("TABLE_NAME", null, null, null, null, null, null); 
    String str = cursor.getString(cursor.getColumnIndex("COLUMN_NAME_YOU_WANT_TO_RETURN")); 
} 

记得关德连接后。这snipet这一切你需要...

+0

不幸的是,这并没有解决它对我来说。我结束了从应用程序清除数据,再次调试,它工作正常。该应用程序保持了一些“旧”数据库不能正常工作。 尽管谢谢你的回复! –

相关问题