jeudi 11 juin 2015

Android How to save msg into local storage?

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!

Aucun commentaire:

Enregistrer un commentaire