因此,我正在製作一個應用程序來記錄聲音,然後將它們添加到另一個活動的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();
}
});
我已經做了很多的研究,但我找不到任何地方如何保存列表視圖狀態。我也試過使用共享偏好設置,但是我非常不成功。我甚至沒有接近工作,所以我刪除了代碼(否則我會把它放在那裏)。如果有人能夠給我一些關於如何將添加的行保存到列表視圖的指針,我會非常感激。
你不應該想着節省,但數據的ListView適配器。如果你保存數據並在列表視圖中重新設置,你將保存你的列表視圖狀態 –