ListView gets empty when back icon is clicked

冷暖自知 提交于 2019-12-11 18:24:17

问题


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.

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

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.

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.

Here is the main activity code,

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

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?

Any help is appreciated.

Many thanks.


回答1:


create a class Constantss.java and declare

public static ArrayList <String> itemList=null;

then inside your class

use

Constantss. itemList.add(name);

instead of

itemList.add(name);

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




回答2:


I believe this is a navigation issue. So you should close your ListActivity like this:

public class ListActivity extends ActionBarActivity {

    //your code

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int itemID = item.getItemId();

        switch (itemID ) {
            case android.R.id.home:
                this.finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Anyway your data can be lost if android destroys your MainActivity. So if you navigate from ListActivity back to MainActivity, MainActivity will be recreated and you data get lost. So you should find the way how to save your data permanent.

there are two common ways:

  1. Save data to data base (SQLite)
  2. Save data to Preferences How to store list of values in SharedPreferences


来源:https://stackoverflow.com/questions/29229153/listview-gets-empty-when-back-icon-is-clicked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!