0

I'm making an app which will have a ListView and a Button to add new new items to the ListView. What I want to do is that when the user will click the Button an AlertDialog containing a TextView and two Buttons will popup. The user will enter the name of the item in the TextView and click the OK Button to add that item to the ListView.

I can do all above said things using this code -

package com.Swap.StudyBuddy;

import java.util.ArrayList;

import android.os.Bundle;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;

import android.content.DialogInterface;

import android.graphics.Typeface;

import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;

import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class Add_Topics extends Activity {

AddTopics_MenuAdapter adapter;
ListView topics_list;
ArrayList<String> the_subjects = new ArrayList<String>();
String new_subject;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add__topics);

    topics_list = (ListView) findViewById(R.id.topics_list);

    the_subjects.add("History");
    the_subjects.add("Geography");

    overridePendingTransition(R.anim.reverse_in_left, R.anim.reverse_out_left);

    Animation enter = AnimationUtils.loadAnimation(getBaseContext(), R.anim.upcoming_menu);
    Animation enter_slow = AnimationUtils.loadAnimation(getBaseContext(), R.anim.enter_l2r_slide);

    TextView des = (TextView)findViewById(R.id.des_at);
    TextView title = (TextView)findViewById(R.id.title_at);

    Button add_topic = (Button)findViewById(R.id.add_topic_button);

    Typeface roboto_lt = Typeface.createFromAsset(getAssets(), "Roboto-Light.ttf");

    des.setTypeface(roboto_lt);
    title.setTypeface(roboto_lt);
    add_topic.setTypeface(roboto_lt);

    title.startAnimation(enter);
    des.startAnimation(enter_slow);

    adapter = new AddTopics_MenuAdapter(this, the_subjects);
    topics_list.setAdapter(adapter);
}

public void onClickAddTopic(View v) {
    showDialog(0);
}
protected Dialog onCreateDialog(int id) {
    switch(id) {
    case 0: 
        LayoutInflater li = LayoutInflater.from(this);
        View edt_txt_add_tpc = li.inflate(R.layout.add_topics_res, null);
        edt_txt_add_tpc.requestFocus();
        edt_txt_add_tpc.setSelected(true);
        final EditText tpc_nm = (EditText) edt_txt_add_tpc.findViewById(R.id.topic_name);
        Builder bld = new AlertDialog.Builder(this);
        bld.setIcon(R.drawable.ic_launcher);
        bld.setTitle("Add Topic/Subject");
        bld.setView(edt_txt_add_tpc);
        bld.setPositiveButton("OK", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int whichButon) {
                String new_topic_name = tpc_nm.getText().toString();
                new_subject = new_topic_name;
                adapter = null;
                the_subjects.add(new_topic_name);
                adapter = new AddTopics_MenuAdapter(getBaseContext(), the_subjects);
                adapter.notifyDataSetChanged();
            }

        });
        bld.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int whichButton) {

        }
        });
        return bld.create();
    }
    return null;
} 
@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_add__topics, menu);
    return true;
    }


}

But, the modified ListView loses all the dynamically added items as soon as the Activity is reloaded. Somebody please tell me what to do so that the newly added items remain in the ListView even after the Activity is reloaded.

Thanks in Advance!!

Swap
  • 480
  • 2
  • 7
  • 21

3 Answers3

1

ListViews do not persist data automatically. In your onCreate method, you create an ArrayList, add two Strings to it, and create an adapter using this ArrayList - so that is the data that will be displayed in your ListView each time the Activity is created again.

You will need to look at storing the data somewhere, for instance using a SQLite database. This can then be used to populate your ListView via a CursorLoader - see the ListView guide to get started.

Ellis
  • 3,048
  • 2
  • 17
  • 28
1

It depends on how long you want to store them for:

To just save them while for the life of the application, ie when the app restarts they will be clearted use Activity.onPause and Activity.onResume to save the list items to the argument bundle.

If you want them to persist after the app has restarted you could either use preferences or a database to store the values.

CurlyPaul
  • 1,138
  • 1
  • 10
  • 29
0

Try not to instantiate the_subjects at the top of your class. Instantiate it in the create method. Otherwise it's instantiated even on resume I guess

KitAndKat
  • 953
  • 3
  • 14
  • 29
  • That won't work. Because, if I instantiate it in onCreate() method. It'll become "out of scope" in the onClick() method. – Swap Mar 11 '14 at 17:33