有一个与代码没有任何错误。不过,我想建立一个注释部分,其中的名称和注释将被保存在数据库中(SQLite的)。
1)它不会停留在TextView中后,我来自另一个页面回来。 2)实施例,我评论一次,和注释显示。我再次发表评论,它实际上重新显示以前的和新的评论意见。
public class BuyerHome extends AppCompatActivity {
DatabaseHelper2 myDB;
//EditText name,comment;
EditText nameIn;
EditText commentIn;
TextView viewComment;
Button postComment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buyer_home);
nameIn = (EditText) findViewById(R.id.nameInput);
commentIn = (EditText) findViewById(R.id.commentText);
viewComment = (TextView) findViewById(R.id.viewCommentText);
myDB = new DatabaseHelper2(this);
postComment = (Button) findViewById(R.id.buttonComment);
postComment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = nameIn.getText().toString();
String comment = commentIn.getText().toString();
if(nameIn.length()!=0 && commentIn.length()!=0){
AddData(name,comment);
nameIn.setText("");
commentIn.setText("");
}else{
Toast.makeText(BuyerHome.this, "Insert fields",
Toast.LENGTH_SHORT).show();
}
Cursor data = myDB.getContents();
if(data.getCount()==0){
Toast.makeText(BuyerHome.this, "Database empty",
Toast.LENGTH_SHORT).show();
}
else{
while(data.moveToNext()){
viewComment.append(data.getString(1));
viewComment.append(data.getString(2)+"\n");
}
}
//viewComment.append("\n"+name+":"+comment);
}
});
}
public void AddData(String name,String comment){
boolean insertData = myDB.insertData(name,comment);
if(insertData==true){
Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "Fail", Toast.LENGTH_SHORT).show();
}
}
}
1)我想在过去的意见,只要页面可以在那里。 2)我只想显示(在过去的评论顶部的最新评论),而不是过去的评论(又重新显示)与新的评论。
我想也许是使用了较旧评论的第二图建议。您还需要确定最新的,而不是旧的评论的一种手段。
在第二视图下面的例子是一个ListView(这将让你点击一个特定的消息,也许引用它,或许提取名)。
该示例迎合用于确定经由id列的最后发布的评论的两种方法(实际使用作为列名音符_id因为这然后允许用于可以是有利ListView的一个光标适配器)。
为了便于显示返回的onResume方法之后的评论已被重写旅馆的活动。
下面的代码: -
public class DatabaseHelper2 extends SQLiteOpenHelper {
private final static String DBNAME = "buyerdb";
private final static int DBVERSION = 1;
public final static String TBL_COMMENT = "comment";
public final static String COL_COMMENT_ID = BaseColumns._ID;
public final static String COL_COMMENT_NAME = "name";
public final static String COL_COMMENT_COMMENT = "comment";
public final static String COl_COMMENT_TIMESTAMP = "timestamp";
private String crt_tbl_comment = "CREATE TABLE IF NOT EXISTS " + TBL_COMMENT + "(" +
COL_COMMENT_ID + " INTEGER PRIMARY KEY, " +
COL_COMMENT_NAME + " TEXT," +
COL_COMMENT_COMMENT + " TEXT, " +
COl_COMMENT_TIMESTAMP + " TEXT DEFAULT CURRENT_TIMESTAMP" +
")";
SQLiteDatabase mDB;
public DatabaseHelper2(Context context) {
super(context, DBNAME, null, DBVERSION);
mDB = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(crt_tbl_comment);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public long addData(String name, String comment) {
ContentValues cv = new ContentValues();
cv.put(COL_COMMENT_NAME,name);
cv.put(COL_COMMENT_COMMENT,comment);
return mDB.insert(TBL_COMMENT,null,cv);
}
public Cursor getLatestComment() {
return mDB.query(TBL_COMMENT,null,null,null,null,null,COl_COMMENT_TIMESTAMP + " DESC","1");
}
public Cursor getAllButLatestComment() {
String whereclause = COL_COMMENT_ID + " < (SELECT max(" +
COL_COMMENT_ID +
") FROM " + TBL_COMMENT +
")";
return mDB.query(TBL_COMMENT,null,whereclause,null,null,null,COl_COMMENT_TIMESTAMP + " DESC");
}
}
public class BuyerHome extends AppCompatActivity {
EditText nameIn;
EditText commentIn;
TextView viewComment;
Button postComment;
ListView olderComments; //<<<<<<<<<< Added (Listview needs to be added to the layout)
DatabaseHelper2 myDB;
Cursor oldermessages, latestmessage; //<<<<<<<<<< Added
SimpleCursorAdapter sma; //<<<<<<<<<< Added adapter for the ListView
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buyer_home);
nameIn = (EditText) findViewById(R.id.nameInput);
commentIn = (EditText) findViewById(R.id.commentText);
viewComment = (TextView) findViewById(R.id.viewCommentText);
olderComments = (ListView) findViewById(R.id.olderComments); //<<<<<<<<<< Added
myDB = new DatabaseHelper2(this);
postComment = (Button) findViewById(R.id.buttonComment);
postComment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = nameIn.getText().toString();
String comment = commentIn.getText().toString();
if (nameIn.length() != 0 && commentIn.length() != 0) {
myDB.addData(name, comment); //<<<<<<<<<< method starts changed lower case a
nameIn.setText("");
commentIn.setText("");
} else {
Toast.makeText(BuyerHome.this, "Insert fields",
Toast.LENGTH_SHORT).show();
}
displayComments(); //<<<<<<<<<< added to refresh the display according to the latest comments
}
});
}
//<<<<<<<<<< ADDED so will display comments whenever called including returning from another activity
@Override
protected void onResume() {
super.onResume();
displayComments();
}
//<<<<<<<<<< ADDED entire method for displaying comments
private void displayComments() {
latestmessage = myDB.getLatestComment();
if (latestmessage.moveToFirst()) {
viewComment.setText(latestmessage.getString(latestmessage.getColumnIndex(DatabaseHelper2.COL_COMMENT_NAME)) +
" said :- " +
latestmessage.getString(latestmessage.getColumnIndex(DatabaseHelper2.COL_COMMENT_COMMENT)));
} else {
viewComment.setText("");
}
oldermessages = myDB.getAllButLatestComment();
if (sma == null) {
sma = new SimpleCursorAdapter(
this,
android.R.layout.simple_list_item_2,oldermessages,
new String[]{DatabaseHelper2.COL_COMMENT_NAME,DatabaseHelper2.COL_COMMENT_COMMENT},
new int[]{android.R.id.text1,android.R.id.text2},
0
);
olderComments.setAdapter(sma);
} else {
sma.swapCursor(oldermessages);
}
}
}