单击后退图标时,ListView为空 [英] ListView gets empty when back icon is clicked

查看:101
本文介绍了单击后退图标时,ListView为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个activities,主要是我有一个text field和两个buttons,用户可以在text field中输入文本.

I have two activities, in the main one I have a text field and two buttons, the user can enter text in the text field.

当单击添加button时,文本将被添加到某些ArrayList.

When clicking the add button,text will be added to some ArrayList.

单击在列表中显示"按钮时,应打开一个带有自己片段的新活动,并显示一个listview,其中包含用户输入的名称.

When clicking show in list button a new activity with its own fragment should open showing a listview that contains the names that the user has entered.

我使用了ArrayAdapter,并且工作正常.当我处于列表活动中时,当我单击设备的后退按钮时,一切都会顺利进行,并且ArrayList中的数据不会丢失. 如果再次单击在列表中显示,我将找到我第一次输入的旧数据.但是,如果我单击设备屏幕左上角的Android提供的后退图标,则ArrayList变为空,并且当我单击在列表中显示"时,listview将显示为空.

I used ArrayAdapter and this is working fine. When I am in the list activity and when I click the back button of the device everything goes fine and the data in the ArrayList is not lost. If I click show in list again I will find the old data I entered at the first time. But, if I click the back icon provided by Android at the top left of the device screen, the ArrayListbecomes empty and when I click show in list the listview shows as empty.

这是主要的活动代码,

public class MainActivity extends ActionBarActivity {


    String LOG_TAG = this.getClass().getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        final ArrayList<String> itemList= new ArrayList<String>();


        final EditText text = (EditText) findViewById(R.id.text_field);
        text.setHint("Enter name here");
        Button addButton = (Button) findViewById(R.id.add_button);
        Button showButton = (Button) findViewById(R.id.show_button);

        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String name=  text.getText().toString();
                itemList.add(name);
                if (itemList.size()==1){
                    Toast.makeText(getApplicationContext(), "one", Toast.LENGTH_SHORT).show();
                }
                Toast.makeText(getApplicationContext(), "name has been added to the list", Toast.LENGTH_SHORT).show();
           }
        });

        showButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent (getApplicationContext(), ListActivity.class);
                intent.putExtra("list", itemList);
                startActivity(intent);
            }
        });

    }//end oncreate

这是ListActivity及其片段代码

This is the ListActivity and its fragment code,

public class ListActivity extends ActionBarActivity {
    static ArrayList <String> itemList;
    String LOG_TAG = this.getClass().getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        itemList= getIntent().getStringArrayListExtra("list");
        setContentView(R.layout.list_activity);
         if (savedInstanceState==null){
             getSupportFragmentManager().beginTransaction().add(R.id.container, new list()).commit();
         }

    }

    public static class list extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
           View rootView = inflater.inflate(R.layout.fragment_main, container,false);
           ListView listView = (ListView) rootView.findViewById(R.id.list);

            ArrayAdapter <String> adapter= new ArrayAdapter<String>(getActivity(),R.layout.fragment_main, R.id.row, itemList);
            listView.setAdapter(adapter);

            return rootView;

        }
    }


} 

有人可以告诉我为什么会这样吗?我该如何解决这个问题?

Can anyone please tell me why this is happening? How can I solve this issue?

感谢您的帮助.

非常感谢.

推荐答案

创建一个类Constantss.java并声明

create a class Constantss.java and declare

public static ArrayList <String> itemList=null;

然后在您的班级内

使用

Constantss. itemList.add(name);

代替

itemList.add(name);

//用常量替换itemList.您所有课程中的itemList

// replace itemList with Constantss. itemList in all of your classes

这篇关于单击后退图标时,ListView为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆