所以我正在制作一个记录声音的应用程序,然后将它们添加到另一个活动中的列表视图中。录制声音后,会要求用户重命名文件,然后将其直接添加到 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();
}
});
我做了很多研究,但我找不到任何地方如何保存列表视图状态。我也尝试过使用共享首选项,但我很不成功。我什至没有接近工作,所以我删除了代码(否则我会把它放在那里)。如果有人能给我一些关于如何将添加的行保存到列表视图的指示,我将不胜感激。