在内部存储中列出文件只显示文件"即时运行"

时间:2016-08-19 03:43:54

标签: java android

我有一个活动(我们将调用SaverActivity)将文件保存到内部存储。我相当确定这是正常的。然后我有一个片段(ListViewFrag),它是另一个Activity的子节点,它应该列出内部存储器中的所有文件。它今天早些时候工作正常,但我必须改变一些东西,因为它现在只用"即时运行填充ListView。"我非常确定SaverActivity工作正常,因为我保存了几个文件,我确定我没有删除,但是它们没有被显示..

这是我的ListViewFrag:

public class ListViewFrag extends Fragment {


public ListViewFrag() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_list_view_frag, container, false);

    // reference the list view
    ListView listView = (ListView) view.findViewById(R.id.templateFilesList);

    // initialize array list
    ArrayList<String> templateList = new ArrayList<String>();


    // get file directory and make a file[] of them
    File myDir = getActivity().getFilesDir();
    File[] templateFiles = myDir.listFiles();

    // if there's anything in there, file the ArrayList with their names
    if(templateFiles != null){
        for(File file : templateFiles){
            templateList.add(file.getName());
        }
    }

    // adapter for ArrayList
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1,
            templateList);

    listView.setAdapter(arrayAdapter);


    return view;
}

}

ListViewFrag的相关XML:

<ListView
        android:id="@+id/templateFilesList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
>

</ListView>

只是为了确定,这是我的SaverActivity的代码:

    // Here I get the name of the previous activity via intent
    String templateName = getIntent().getExtras().getString("key1");

    // initializing a new 2d array (the object I'm writing to file is that type)
    String[][] doWArray1New = new String[15][15];

    FileOutputStream fout;

    if(ControllerData.getInstance().doWArray1 != null) {
        // Just putting my data (doWArray1 is a 2d array) into my new 2d array
        doWArray1New = ControllerData.getInstance().doWArray1;

        try {
            // adding the "1" is just my naming convention for this file
            fout = openFileOutput(templateName + "1", Context.MODE_PRIVATE);

            ObjectOutputStream oos = new ObjectOutputStream(fout);
            oos.writeObject(doWArray1New); // write it
            oos.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

感谢任何输入!我只是感到困惑,因为我觉得自今天早上起我没有触及过代码,所以我不确定是什么改变了。

0 个答案:

没有答案