简体   繁体   中英

Save listview state (rows)?

So I'm making an app that records sounds and then adds them to listview in another activity. After the sound is recorded, a user is asked to rename the file and then it is added directly to listview.

Adding items to listview code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recorded_library);

    listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, fileNames);
}

public void setFileName(final Editable filename) {
    Log.d("2", "Set filename from first activity " + filename);
    TextView emptyLibText = (TextView) findViewById (R.id.textView1);
    emptyLibText.setVisibility(TextView.INVISIBLE);

    //LISTVIEW
    fileNames.add(filename.toString()); 
    listView = (ListView) findViewById (R.id.mainListView);
    listView.setAdapter(listAdapter);

    //ALERT DIALOG
    final AlertDialog.Builder deleteAlert = new AlertDialog.Builder(this);

    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
            player = new MediaPlayer();
            try {
                player.setDataSource(externalStoragePath + File.separator + "Android/data/com.whizzappseasyvoicenotepad/" + fileNames.get(arg2) + ".mp3");
                player.prepare();
                player.start();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            Toast toast = Toast.makeText(getApplicationContext(), "Now playing: " + fileNames.get(arg2), Toast.LENGTH_SHORT);
            toast.show();
        }
    });

    listView.setOnItemLongClickListener(new OnItemLongClickListener(){

        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            deleteAlert.setTitle("Warning");
            deleteAlert.setMessage("Are you sure you want to delete this?");
            toDelete = arg2;
            deleteAlert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    File directory = new File (externalStoragePath + File.separator + "Android/data/com.whizzappseasyvoicenotepad/");
                    File deleteFile = new File (directory, fileNames.get(toDelete) + ".mp3");
                    deleteFile.delete();
                    Log.i("TAG", "Deleting file: " + directory + fileNames.get(toDelete) + ".mp3");

                    listAdapter.remove(listAdapter.getItem(toDelete));
                    listAdapter.notifyDataSetChanged();

                    dialog.dismiss();
                }
            });

I've done a lot of research but I can't find anywhere how to save listview state. I also tried using Shared Preferences but I was very unsuccessful with it. I didn't even come close to working so I deleted the code (else I'd put it there). I'd appreciate it a lot if someone could give me some pointers on how I could save added rows to listview.

It looks like you're trying to persist the list data on the device (rather than fetching it from a server). You should check out the Storage Options section in the guide.

The easiest (but not necessarily the proper) way to do this is to dumped the serialized ArrayList into Internal Storage . Here's a snippet of what it may look like (not tested):

ArrayList<String> filenames = ...
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(filenames);
oos.close();

PS. Several recommendations for your code:

  1. Setup the ListView (setAdapters, setOnItemClickListener) in onCreate instead of in setFileName().
  2. Try using setEmptyView() instead of emptyLibText

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