简体   繁体   中英

Android - saving checked items of a ListView on app shutdown

I am making an application for the Android platform in Eclipse, and I need help with something. :) What I want for one part to do is create an arraylist from items that are checked in another ListView. I figured out how to make that and here is the code:

public class MusicList extends Activity {
    private ListView lvCheckBox;
    private String[] arr = { "Depeche Mode", "The Prodigy", "Rammstein",
            "Manilla Road", "Led Zeppelin", "AC/DC", "Massive Attack",
            "Skrillex", "Deadmau5" };
    ArrayList<String> arrList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.musiclist);
        lvCheckBox = (ListView) findViewById(R.id.lvCheckBox);
        lvCheckBox.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        lvCheckBox.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_checked, arr));
        arrList = new ArrayList<String>();
        lvCheckBox.setOnItemClickListener(new OnItemClickListener(){
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {
                    // TODO Auto-generated method stub
                     if(arrList.contains(lvCheckBox.getItemAtPosition(arg2).toString()))
                    {
                        arrList.remove(lvCheckBox.getItemAtPosition(arg2).toString());
                    }
                    else
                    {
                        arrList.add(lvCheckBox.getItemAtPosition(arg2).toString());
                    }

                    Collections.sort(arrList);
                    String strText = "";

                    for(int i=0 ; i<arrList.size(); i++)                   
                           strText += arrList.get(i) + ",";


                    Toast.makeText(MusicList.this, "Item Clicked: "+ strText,     Toast.LENGTH_SHORT).show();                
                }
        });
    }
}

BUT! Now I need to save that arraylist (like some "user settings" (actually the music which the user likes)) even when the app closes completely and when I come back to the list screen previously checked items need to be checked again (bear in mind I will be adding a lot more musicians to the starting list!!). So, anyone knows how to do that? :)

That's all, thanks in advance, and also I am new here so sorry if I messed something up :(

Android has a SharedPreferences class that will allow you to save information to the App's "cache".

So, you'd be able to save and retrieve the list information.

http://developer.android.com/reference/android/content/SharedPreferences.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM