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!!