Android 如何将消息保存到本地存储?
Android How to save msg into local storage?
我是 Android 开发人员的新手。我目前正在制作一个简单的消息应用程序。所以我想存储我从节点服务器收到的所有短信。我现在真的不知道该怎么做。
我从节点服务器收到的消息是 JSONObject,如:
{"name":"XX", "id":"XX","message":"xxxxxxxx"}
我正在使用自定义 ArrayAdepter 来显示文本:
public class ChatArrayAdapter extends ArrayAdapter<Text> {
private TextView text,avatar;
private List<Text> msgcontx = new ArrayList<Text>();
private LinearLayout wrapper;
@Override
public void add(Textobject){
msgcontx.add(object);
super.add(object);
}
public ChatArrayAdapter(Context context,int textViewResourceId) {
super(context, textViewResourceId);
}
public int getCount(){
return this.msgcontx.size();
}
public Text getItem(int index){
return this.msgcontx.get(index);
}
public View getView(int position,View convertView,ViewGroup parent){
View row = convertView;
if(row == null){
LayoutInflater inflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.chat_listview, parent,false);
}
wrapper = (LinearLayout)row.findViewById(R.id.wrapper);
Text comment = getItem(position);
avatar = (TextView)row.findViewById(R.id.avatar);
avatar.setText(comment.userID);
avatar.setVisibility(comment.left? View.VISIBLE: View.GONE);
text = (TextView)row.findViewById(R.id.comment);
text.setText(comment.comment);
// chat background to be changed
text.setBackgroundResource(comment.left ? R.drawable.bg_chat_recipient : R.drawable.bg_chat_sender );
wrapper.setGravity(comment.left? Gravity.START : Gravity.END);
return row;
}
public Bitmap decodeToBitmap(byte[] decodedByte){
return BitmapFactory.decodeByteArray(decodedByte, 0,decodedByte.length );
}
}
现在我不知道应该如何将从节点服务器收到的消息存储在本地存储中,并且当我打开应用程序时,我还想显示消息历史记录。
我应该使用SQL吗?但我应该如何存储味精?把它们放在不同的行?
我知道这可能是一个愚蠢的问题,但我真的不知道如何将消息存储到本地存储并再次读取它们。
谁能简单介绍一下?非常感谢!
你应该使用某种 database.I 推荐的带有 orm 的 sql 数据库 - 它可以让你创建一个基于 classes 的数据库并消除大量 sql.
喜欢active android,也看看sugar orm
您应该有一条消息 table,并且 table 中的每一行都是一个 message.In 您使用 class 定义消息的 orm。示例代码(使用 ActiveAndroid orm):
@Table(name="Messages")
class Message extends Model {
String name;
String id;
String message;
//the empty consructor is required by active android
public Message(){
super();
}
}
要将消息实际输入数据库,您需要解析 json 并从中创建 Messages 对象,然后 them.To 将 json 解析为 Message 对象可以使用 Gson 或者 logan square.
我是 Android 开发人员的新手。我目前正在制作一个简单的消息应用程序。所以我想存储我从节点服务器收到的所有短信。我现在真的不知道该怎么做。
我从节点服务器收到的消息是 JSONObject,如:
{"name":"XX", "id":"XX","message":"xxxxxxxx"}
我正在使用自定义 ArrayAdepter 来显示文本:
public class ChatArrayAdapter extends ArrayAdapter<Text> {
private TextView text,avatar;
private List<Text> msgcontx = new ArrayList<Text>();
private LinearLayout wrapper;
@Override
public void add(Textobject){
msgcontx.add(object);
super.add(object);
}
public ChatArrayAdapter(Context context,int textViewResourceId) {
super(context, textViewResourceId);
}
public int getCount(){
return this.msgcontx.size();
}
public Text getItem(int index){
return this.msgcontx.get(index);
}
public View getView(int position,View convertView,ViewGroup parent){
View row = convertView;
if(row == null){
LayoutInflater inflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.chat_listview, parent,false);
}
wrapper = (LinearLayout)row.findViewById(R.id.wrapper);
Text comment = getItem(position);
avatar = (TextView)row.findViewById(R.id.avatar);
avatar.setText(comment.userID);
avatar.setVisibility(comment.left? View.VISIBLE: View.GONE);
text = (TextView)row.findViewById(R.id.comment);
text.setText(comment.comment);
// chat background to be changed
text.setBackgroundResource(comment.left ? R.drawable.bg_chat_recipient : R.drawable.bg_chat_sender );
wrapper.setGravity(comment.left? Gravity.START : Gravity.END);
return row;
}
public Bitmap decodeToBitmap(byte[] decodedByte){
return BitmapFactory.decodeByteArray(decodedByte, 0,decodedByte.length );
}
}
现在我不知道应该如何将从节点服务器收到的消息存储在本地存储中,并且当我打开应用程序时,我还想显示消息历史记录。
我应该使用SQL吗?但我应该如何存储味精?把它们放在不同的行? 我知道这可能是一个愚蠢的问题,但我真的不知道如何将消息存储到本地存储并再次读取它们。
谁能简单介绍一下?非常感谢!
你应该使用某种 database.I 推荐的带有 orm 的 sql 数据库 - 它可以让你创建一个基于 classes 的数据库并消除大量 sql.
喜欢active android,也看看sugar orm
您应该有一条消息 table,并且 table 中的每一行都是一个 message.In 您使用 class 定义消息的 orm。示例代码(使用 ActiveAndroid orm):
@Table(name="Messages")
class Message extends Model {
String name;
String id;
String message;
//the empty consructor is required by active android
public Message(){
super();
}
}
要将消息实际输入数据库,您需要解析 json 并从中创建 Messages 对象,然后 them.To 将 json 解析为 Message 对象可以使用 Gson 或者 logan square.