Android How to save msg into local storage?

1.3k views Asked by At

I'm pretty new to the Android developer. I'm currently making an simple message app. so i want to store all the text message I received from node server. I don't really now how should i do this.

the msg I received from node server is JSONObject like:

{"name":"XX", "id":"XX","message":"xxxxxxxx"}

and I'm using a Custom ArrayAdepter to display the text:

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 );

  }
}

now I don't know how should I store those msg I received from node server at local storage, and when i open the app, I want to also display the msg history.

should I use SQL? but how should I store the msg? put them in different rows? I know it might be a dumb question, But I really don't know how to store the msg to local storage and read them again.

Anyone can give a brief introduction? Thanks a lot!

1

There are 1 answers

3
user3629774 On BEST ANSWER

You should use some kind of database.I recommend an sql db with an orm - it lets you create a database based on classes and eliminates the need in ton of sql.

I like active android, but also take a look at sugar orm.

You should have a messages table, and each row in the table will be a message.In an orm you define the message using a class. example code (using 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();
    }

}

to actually enter the messages into the databse you will need to parse the json and create Messages objects from them, then save them.To parse the json into the Message object you can use Gson or logan square.