1

i want to add listview item dynamically. i had done this but when i restart the app listview item disappear. How can i save this item permanently to listview. please suggest

public class ChatWithAttorney extends Activity {
    ImageView btnBack;
    ListView chatList;
    EditText etMsg;
    Button btnSend;
    String msg_to_send;
    ArrayAdapter<String> adapter;
    ArrayList<String> listMsg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chatwithattorny);
        btnBack = (ImageView) findViewById(R.id.btnback);
        chatList = (ListView) findViewById(R.id.chatList);
        etMsg = (EditText) findViewById(R.id.etMsg);
        btnSend = (Button) findViewById(R.id.btnSend);
        listMsg = new ArrayList<String>();
        adapter = new ArrayAdapter<String>(this, R.layout.chat_list_text, R.id.tvSentMsg,listMsg);
        chatList.setAdapter(adapter);
        btnSend.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                try {
                    msg_to_send = URLEncoder.encode(etMsg.getText().toString(),
                            "utf-8");
                    listMsg.add(msg_to_send);
                    adapter.notifyDataSetChanged();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        });
    }
mdDroid
  • 3,135
  • 2
  • 22
  • 34
Centreonyx
  • 34
  • 3
  • use local database for storing the messages – mdDroid May 25 '16 at 12:58
  • Your question is about persistent storage, not adding dynamic elements... That being said, there is a well written page called [storage options](https://developer.android.com/guide/topics/data/data-storage.html) – OneCricketeer May 25 '16 at 12:58
  • Hello please check this link you will get your answer [Click Here](http://stackoverflow.com/questions/4540754/dynamically-add-elements-to-a-listview-android) – Jinal Patel May 25 '16 at 13:00

3 Answers3

0

Put those ArrayList items in a database or SharedPreference and read those items when resuming your activity.

dumb_terminal
  • 1,815
  • 1
  • 16
  • 27
  • can you please explain i an new to android. i have not used any database yet. – Centreonyx May 25 '16 at 13:04
  • search for android sqlite or shared preference. they keeps your data saved so that you can reuse your data later. – dumb_terminal May 25 '16 at 13:07
  • and one more thing how can i add item at bottom of listview and auto scroll up when new message comes – Centreonyx May 25 '16 at 13:09
  • when adding to to list you can specify position. like list.add(pos, item). also lookfor listview methods. list view or adapter should have a scrollTo method which takes a position as param and scrolls to that item. – dumb_terminal May 25 '16 at 13:12
0

You can use Local database , SharedPreference or Static arraylist for storing the messages

mdDroid
  • 3,135
  • 2
  • 22
  • 34
0

AT the Time of Destroy Serialize the List data. And Deserilize when Application Starts Again.

Shubham
  • 521
  • 4
  • 11