希望你做得很好。我需要一些经验帮助来完成我的基于asmack的聊天应用程序,我需要知道哪个是保持聊天记录历史的最佳方式,并根据要求保留给ui,我已经做到了数据库,但我根本不满足于它,它会大大降低页面打开速度,我也可以使用SharedPreferences,但我没有任何相关经验。
我真的很感激,如果你能告诉我一个关于所有聊天应用程序用于保存和保留聊天记录的方式的提示,或者哪种方式最好这样做?
非常感谢。
这是我创建或打开数据库的方式:
db = openOrCreateDatabase("singlechat", Context.MODE_PRIVATE, null);
这是我插入数据库的方式(我为每个新用户创建一个新表)
final String Text = event.text;
final String Type = "m";
final String name = event.name;
final Blob OtherFormats = null;
final String Date = event.itime;
final Blob Image = null;
final String RealName = "s";
final String ID = "" ;
Thread t = new Thread(new Runnable() {
@Override
public void run() {
String sqlCreate = "CREATE TABLE IF NOT EXISTS " + name
+ " (ID INTEGER PRIMARY KEY AUTOINCREMENT,"
+ " Type TEXT,"
+ " Text TEXT,"
+ " OtherFormats BLOB,"
+ " Date TEXT,"
+ " Image BLOB,"
+ " RealName TEXT)";
db.execSQL(sqlCreate);
String sqlInsert= "INSERT INTO " + "'"+name+"'"+"
(Type,Text,OtherFormats,Date,Image,RealName)VALUES
("+"'"+Type+"'"+","+"'"+Text+"'"+","+"'"+
OtherFormats+"'"+","+"'"+Date+"'"+","+"'"+Image+"'"+","+"'"+RealName+"')";
db.execSQL(sqlInsert);
}
});
t.start();
}
这是一个简单的数组适配器:
public static ArrayList<String> messages = new ArrayList<String>();
public static Handler mHandler = new Handler();
private static ListView listview;
public void setListAdapter() {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.listitem, messages);
listview.setAdapter(adapter);
}