Android-ListView没有';不保留动态添加的项目

Android-ListView没有';不保留动态添加的项目,android,listview,android-listview,android-alertdialog,savechanges,Android,Listview,Android Listview,Android Alertdialog,Savechanges,我正在制作一个应用程序,它将有一个列表视图和一个按钮,用于向列表视图添加新项目。 我想做的是,当用户单击按钮时,将弹出一个警报对话框,其中包含一个文本视图和两个按钮。用户将在文本视图中输入项目名称,然后单击确定按钮将该项目添加到列表视图 我可以使用此代码完成上述所有操作- package com.Swap.StudyBuddy; import java.util.ArrayList; import android.os.Bundle; import android.app.Activity

我正在制作一个应用程序,它将有一个
列表视图
和一个
按钮
,用于向
列表视图
添加新项目。 我想做的是,当用户单击
按钮时,将弹出一个
警报对话框
,其中包含一个
文本视图
和两个
按钮
。用户将在
文本视图
中输入项目名称,然后单击确定
按钮
将该项目添加到
列表视图

我可以使用此代码完成上述所有操作-

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


}
package com.Swap.StudyBuddy;
导入java.util.ArrayList;
导入android.os.Bundle;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.app.AlertDialog.Builder;
导入android.app.Dialog;
导入android.content.DialogInterface;
导入android.graphics.Typeface;
导入android.view.LayoutInflater;
导入android.view.Menu;
导入android.view.view;
导入android.view.animation.animation;
导入android.view.animation.AnimationUtils;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.ListView;
导入android.widget.TextView;
公共类添加主题扩展活动{
添加菜单适配器;
ListView主题列表;
ArrayList_subjects=new ArrayList();
串新主题;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u add\u主题);
topics\u list=(ListView)findViewById(R.id.topics\u list);
加上(“历史”);
加上(“地理”);
覆盖转换(R.anim.reverse\u in\u left,R.anim.reverse\u out\u left);
Animation enter=AnimationUtils.loadAnimation(getBaseContext(),R.anim.uncoming_菜单);
动画输入\u slow=AnimationUtils.loadAnimation(getBaseContext(),R.anim.enter\u l2r\u slide);
TextView des=(TextView)findViewById(R.id.des_at);
TextView title=(TextView)findViewById(R.id.title\u at);
按钮添加主题=(按钮)findViewById(R.id.add\u topic\u按钮);
Typeface roboto_lt=Typeface.createFromAsset(getAssets(),“roboto Light.ttf”);
des.setTypeface(roboto_lt);
标题.设置字体(roboto_lt);
添加主题设置字体(roboto lt);
标题。开始激活(输入);
des.startAnimation(缓慢输入);
adapter=newaddtopics\u MenuAdapter(这是主题);
主题列表。设置适配器(适配器);
}
公共void onClickAddTopic(视图v){
显示对话框(0);
}
受保护的对话框onCreateDialog(int id){
开关(id){
案例0:
LayoutInflater li=LayoutInflater.from(this);
查看edt\u txt\u add\u tpc=li.inflate(R.layout.add\u topics\u res,null);
edt_txt_add_tpc.requestFocus();
edt_txt_add_tpc.setSelected(true);
最终编辑文本tpc_nm=(编辑文本)edt_txt_add_tpc.findViewById(R.id.topic_name);
Builder bld=新建AlertDialog.Builder(此);
bld.设置图标(R.可牵引ic_发射器);
bld.设置标题(“添加主题/主题”);
bld.setView(edt\U txt\U add\U tpc);
bld.setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int whichButon){
字符串new_topic_name=tpc_nm.getText().toString();
新建主题=新建主题名称;
适配器=空;
添加(新主题名称);
adapter=newaddtopics\u MenuAdapter(getBaseContext(),主题);
adapter.notifyDataSetChanged();
}
});
bld.setNegativeButton(“取消”,新建DialogInterface.OnClickListener(){
public void onClick(对话框接口对话框,int whichButton){
}
});
返回bld.create();
}
返回null;
} 
@凌驾
public void onBackPressed(){
super.onBackPressed();
覆盖转换(R.anim.slide\u in\u left,R.anim.slide\u out\u left);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(R.menu.activity\u add\u主题,菜单);
返回true;
}
}
但是,只要重新加载
活动
,修改后的
列表视图
就会丢失所有动态添加的项。有人请告诉我如何做,以便即使在重新加载
活动之后,新添加的项目仍保留在
列表视图中


提前谢谢

尽量不要在你的班级中列举最重要的科目。在create方法中实例化它。否则即使在简历上也会实例化,我想列表视图不会自动保存数据。在
onCreate
方法中,创建一个ArrayList,向其中添加两个字符串,并使用该ArrayList创建一个适配器-因此,每次再次创建活动时,将在ListView中显示该数据


例如,您需要查看使用SQLite数据库的情况。然后,可以使用它通过游标加载程序来填充ListView,请参见开始部分。

这取决于您要将它们存储多长时间:

要在应用程序生命周期内保存它们,即当应用程序重新启动时,它们将被清除,请使用Activity.onPause和Activity.onResume将列表项保存到参数包中


如果你想让它们在应用程序重新启动后继续存在,你可以使用首选项或数据库来存储这些值。

这行不通。因为,如果我在onCreate()方法中实例化它。在onClick()方法中,它将变为“超出范围”。