2013-08-06 166 views
1

因此,我正在製作一個應用程序來記錄聲音,然後將它們添加到另一個活動的listview中。錄製完聲音後,會要求用戶重命名文件,然後將其直接添加到列表視圖中。保存列表狀態(行)?

添加項目列表視圖代碼:

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

我已經做了很多的研究,但我找不到任何地方如何保存列表視圖狀態。我也試過使用共享偏好設置,但是我非常不成功。我甚至沒有接近工作,所以我刪除了代碼(否則我會把它放在那裏)。如果有人能夠給我一些關於如何將添加的行保存到列表視圖的指針,我會非常感激。

+0

你不應該想着節省,但數據的ListView適配器。如果你保存數據並在列表視圖中重新設置,你將保存你的列表視圖狀態 –

回答

1

看起來你試圖在設備上保存列表數據(而不是從服務器獲取)。您應該查看指南中的Storage Options部分。

最簡單的(但不一定是正確的)方法是將序列化的ArrayList轉儲到Internal Storage。下面是它可能看起來像(未測試)的片段:

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

PS。爲您的代碼有幾個建議:

  1. 設置中的onCreate,而不是在setFileName ListView控件(setAdapters,setOnItemClickListener)()。
  2. 嘗試使用setEmptyView()代替emptyLibText
+0

非常感謝我告訴我關於setEmptyListView(),我正在尋找類似的東西:)實施IF語句只是爲了設置可見性也是如此很多工作,這是更容易,我仍然不知道我將如何存儲列表視圖數據,但你的代碼混淆了我,但我會盡我所能,使其工作:) – Guy