[英]Refresh ListView after returning from another activity through finish()
我正在开发一个Android应用程序,其中有一个扩展ListActivity的类,该类使用ArrayAdapter从SD卡中的目录填充文件。 它在其OnItemClickListener()上调用另一个活动,在该活动中,我通过单击按钮从同一目录中删除文件,并在删除相同的删除按钮后调用finish()。 当控件返回到ListActivity时,我希望更新列表。 我已经尝试过在此站点上给出的几种方法,但没有一种对我有用。 解决该问题的任何帮助将不胜感激。 下面是我的代码:
public class NoteList extends ListActivity {
String userDataFolder;
String userDisplayName;
String userEmailID;
String currTimeSlot;
String currDate;
String chosenDate;
Bundle b = null;
public static String curDate = "";
ArrayList<File> listOfNoteFiles = new ArrayList<File>();
ArrayList<String> listOfNoteNames = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.note_list);
Intent intent = getIntent();
userDataFolder = intent.getStringExtra("userName");
userDisplayName = intent.getStringExtra("displayUserName");
userEmailID = intent.getStringExtra("thisUserid");
currTimeSlot = intent.getStringExtra("currentTimeSlot");
currDate = intent.getStringExtra("currentDate");
chosenDate = intent.getStringExtra("chosenDate");
@SuppressWarnings("deprecation")
Date curDateTime = new Date(chosenDate);
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy");
String ChosenFolderDate = formatter.format(curDateTime);
formatter = new SimpleDateFormat("MMM dd, yy");
if (!Environment.getExternalStorageState().equals(Environment
.MEDIA_MOUNTED))
{
//handles case of no SDCARD present
Toast.makeText(this, "Please insert an SD Card",
Toast.LENGTH_LONG).show();
}
else
{
File notesFolder = new File(Environment
.getExternalStorageDirectory()
.getAbsolutePath()+"/MyMoodDiary/"
+userDataFolder+"/MoodNotes/"+ChosenFolderDate+
"/"+currTimeSlot+File.separator);
if(notesFolder.exists() && notesFolder.isDirectory())
{
fillData(notesFolder);
}
}
setListAdapter(new ArrayAdapter<String>(this, R.layout.notes_list_item,
listOfNoteNames));
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
Intent intent = new Intent(NoteList.this, Read_Notes.class);
b = new Bundle();
b.putString("noteNameAndPath",
listOfNoteFiles.get(position).toString());
b.putString("userName", userDataFolder);
b.putString("displayUserName", userDisplayName);
b.putString("thisUserid", userEmailID);
b.putString("currentTimeSlot", currTimeSlot);
b.putString("chosenDate", chosenDate);
intent.putExtras(b);
startActivity(intent);
}
});
registerForContextMenu(getListView());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.notelist_menu, menu);
return true;
}
private void fillData(File notesFolder)
{
File[] files = notesFolder.listFiles();
for (File file : files)
{
if(file.isFile() && isTXT(file))
{
listOfNoteFiles.add(file);
int index = (file.toString()).lastIndexOf( '/' );
listOfNoteNames.add((file.toString()).substring(index+1));
}
else if(file.isDirectory())
{
fillData(file.getAbsoluteFile());
}
}
}
private boolean isTXT(File file)
{
boolean is = false;
if(file.getName().endsWith(".txt"))
{
is = true;
}
return is;
}
}
如果使用startActivityForResult()
启动Read_Notes.class
则覆盖NoteList。 Read_Notes
完成时将通知onActivityResult()
。 有关详细信息,请参见从活动获取结果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.