通过finish()从另一个活动返回后刷新ListView

时间:2015-10-12 10:43:54

标签: android listview android-arrayadapter onresume activity-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;
    }
}

1 个答案:

答案 0 :(得分:0)

如果您使用startActivityForResult()启动Read_Notes.class覆盖NoteList。onActivityResult()将在Read_Notes完成时收到通知。有关详细信息,请参阅 Getting a Result from an Activity