我想在listView上显示之前更改字段的值。
这是我想要执行更改的代码部分:
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Cursor c = getCursor();
final LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.messagemenu_row_layout, parent, false);
int nameCol = c.getColumnIndex(MessageMenuDBHandler.COLUMN_NAME_DT);
Long millis = c.getLong(nameCol);
millis = millis * 1000;
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
Date resultdate = new Date(millis);
String fdt = sdf.format(resultdate);
Log.i(TAG, "Formatted DT: " + fdt);
TextView textView_DT = (TextView) v.findViewById(R.id.list_dt);
textView_DT.setText("test" + fdt);
return v;
}
listView上显示的数据没有改变,它仍然显示旧的时间戳格式,类似于' 1423300445'。
Millis DT:1423300445000 格式化DT:Feb 07,2015 04:14
在将数据发送到listView之前,我该怎么做才能更改数据?
谢谢!
== EDIT1 == 我插入记录的部分:
public void onClick_addRecord(View V){
///// Add to MessageMenuDB /////
// Prepare database
MessageMenuDBHandler dbHandler = new MessageMenuDBHandler(context, null, null, 1);
Long tsLong = System.currentTimeMillis()/1000;
int ts = Integer.parseInt(tsLong.toString());
String msg = "Sample messsage";
String type = "Type";
String dest = "Dest";
String from = "From";
Log.i(TAG, "ts: " + String.valueOf(ts));
//(int dt, String message, String type, String dest, String from, int seen, int sent, int mo)
MessageMenu messageMenu = new MessageMenu(
ts, msg, type, dest, from, 0, 0, 0
);
dbHandler.addMessage(messageMenu);
generateMessageList();
}
== EDIT2 == 数据库调用
public class MessageMenuDBHandler extends SQLiteOpenHelper {
public static final String TAG = "DBHandler";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "MessageMenuDB.db";
public static final String TABLE_NAME = "messages";
public static final String COLUMN_NAME_ID = "_id";
public static final String COLUMN_NAME_DT = "dt";
public static final String COLUMN_NAME_MESSAGE = "message";
public static final String COLUMN_NAME_TYPE = "type";
public static final String COLUMN_NAME_DEST = "dest";
public static final String COLUMN_NAME_FROM = "frm";
public static final String COLUMN_NAME_SEEN = "seen"; // 2-way, Seen by us or by recipient, 0 or 1
public static final String COLUMN_NAME_SENT = "sent"; // message sent to server, 0 or 1
public static final String COLUMN_NAME_MO = "mo"; // Message Originating, 1 if message was made by us
public MessageMenuDBHandler(Context context, String name,
SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
//db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
String CREATE_PRODUCTS_TABLE = "CREATE TABLE " +
TABLE_NAME + "("
+ COLUMN_NAME_ID + " INTEGER PRIMARY KEY,"
+ COLUMN_NAME_DT + " INTEGER,"
+ COLUMN_NAME_MESSAGE + " TEXT,"
+ COLUMN_NAME_TYPE + " TEXT,"
+ COLUMN_NAME_DEST + " TEXT,"
+ COLUMN_NAME_FROM + " TEXT,"
+ COLUMN_NAME_SEEN + " INTEGER,"
+ COLUMN_NAME_SENT + " INTEGER,"
+ COLUMN_NAME_MO + " INTEGER" + ")";
db.execSQL(CREATE_PRODUCTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public void addMessage(MessageMenu messageMenu) {
ContentValues values = new ContentValues();
values.put(COLUMN_NAME_DT, messageMenu.getDT());
values.put(COLUMN_NAME_MESSAGE, messageMenu.getMessage());
values.put(COLUMN_NAME_TYPE, messageMenu.getType());
values.put(COLUMN_NAME_DEST, messageMenu.getDest());
values.put(COLUMN_NAME_FROM, messageMenu.getFrom());
values.put(COLUMN_NAME_SEEN, messageMenu.getSeen());
values.put(COLUMN_NAME_SENT, messageMenu.getSent());
values.put(COLUMN_NAME_MO, messageMenu.getMO());
Log.i(TAG, "dt: " + messageMenu.getDT());
Log.i(TAG, "msg: " + messageMenu.getMessage());
Log.i(TAG, "type: " + messageMenu.getType());
Log.i(TAG, "dest: " + messageMenu.getDest());
Log.i(TAG, "from: " + messageMenu.getFrom());
Log.i(TAG, "seen: " + messageMenu.getSeen());
Log.i(TAG, "sent: " + messageMenu.getSent());
Log.i(TAG, "mo: " + messageMenu.getMO());
SQLiteDatabase db = this.getWritableDatabase();
db.insert(TABLE_NAME, null, values);
db.close();
}
public boolean deleteMessage(int id) {
boolean result = false;
String query = "Select * FROM " + TABLE_NAME + " WHERE " + COLUMN_NAME_ID + " = " + id + "";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
MessageMenu messageMenu = new MessageMenu();
if (cursor.moveToFirst()) {
messageMenu.setID(Integer.parseInt(cursor.getString(0)));
db.delete(TABLE_NAME, COLUMN_NAME_ID + " = ?",
new String[] { String.valueOf(messageMenu.getID()) });
cursor.close();
result = true;
}
db.close();
return result;
}
public Cursor query(String tableName, String orderedBy) {
String[] projection = {COLUMN_NAME_ID, COLUMN_NAME_DT, COLUMN_NAME_MESSAGE,
COLUMN_NAME_TYPE, COLUMN_NAME_DEST, COLUMN_NAME_FROM,
COLUMN_NAME_SEEN, COLUMN_NAME_SENT, COLUMN_NAME_MO};
//String[] projection = {COLUMN_NAME_PRODCODE, COLUMN_NAME_DETAIL};
Cursor c = getReadableDatabase().query(tableName, projection, null, null, null, null, orderedBy);
//getReadableDatabase().close();
return c;
}
}
但我想在显示之前修改数据,而不是在保存之前,因为在我的实际应用程序中,数据已经存在,已保存并且我不允许更改,我只能改变我显示数据的方式。
== EDIT3 == 这就是我填充listView
的方式private void generateMessageList(){
MessageMenuDBHandler dbHandler = new MessageMenuDBHandler(context, null, null, 1);
Cursor c = dbHandler.query(MessageMenuDBHandler.TABLE_NAME,
MessageMenuDBHandler.COLUMN_NAME_ID + " DESC");
String[] from = new String[]{MessageMenuDBHandler.COLUMN_NAME_FROM,
MessageMenuDBHandler.COLUMN_NAME_MESSAGE,
MessageMenuDBHandler.COLUMN_NAME_DT};
int[] to = {
R.id.list_from,
R.id.list_msg,
R.id.list_dt
};
MessageMenuAdapter mySimpleCursorAdapter = new MessageMenuAdapter(
this,
//android.R.layout.simple_list_item_1,
R.layout.messagemenu_row_layout,
c,
from,
to,
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
listView.setAdapter(mySimpleCursorAdapter);
listItemClickListener();
}
== EDIT4 == 所以我尝试按照@DerGolem的建议编辑查询,但是我对如何将新值返回到游标变量c有点迷失。试图查看代码完成c。看看我能用的东西,但我所看到的大部分都是......功能。任何提示都将不胜感激。
public Cursor query(String tableName, String orderedBy) {
String[] projection = {COLUMN_NAME_ID, COLUMN_NAME_DT, COLUMN_NAME_MESSAGE,
COLUMN_NAME_TYPE, COLUMN_NAME_DEST, COLUMN_NAME_FROM,
COLUMN_NAME_SEEN, COLUMN_NAME_SENT, COLUMN_NAME_MO};
//String[] projection = {COLUMN_NAME_PRODCODE, COLUMN_NAME_DETAIL};
Cursor c = getReadableDatabase().query(tableName, projection, null, null, null, null, orderedBy);
//getReadableDatabase().close();
int nameCol = c.getColumnIndex(MessageMenuDBHandler.COLUMN_NAME_DT);
Long millis = c.getLong(nameCol);
millis = millis * 1000;
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
Date resultdate = new Date(millis);
String fdt = sdf.format(resultdate);
/* now how am I supposed to get the value of fdt back to that c? xD */
return c;
}
答案 0 :(得分:0)
我的第一种方法让它变得复杂。而不是我的旧代码:
MessageMenuAdapter mySimpleCursorAdapter = new MessageMenuAdapter(
this,
R.layout.messagemenu_row_layout,
c,
from,
to,
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
listView.setAdapter(mySimpleCursorAdapter);
我只是坚持使用SimpleCursorAdaper而没有自定义的MessageMenuAdapter。用以下代码替换代码:
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.messagemenu_row_layout,
c,
from,
to);
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (columnIndex == 1) {
Long millis = cursor.getLong(columnIndex);
millis = millis * 1000;
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
Date resultdate = new Date(millis);
String fdt = sdf.format(resultdate);
((TextView) view).setText(fdt);
return true;
}
return false;
}
});
listView.setAdapter(adapter);
解决方案是setViewBinder。至少这适用于我的特定问题。
感谢@DerGolem非常耐心地提出解决方案。