简体   繁体   中英

Read a file in a fragment and put it in a listview

I have a "Todo.txt" file where i want to read all lines and insert them to my listview. It seems like the listview is not getting any items and the problem is how i read the file in. Any suggestions?

public class MainFragment extends Fragment {
    ArrayList<String> arrayList = new ArrayList<String>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_main, container, false);

        // Set title of fragment page
        TextView textView = (TextView) v.findViewById(R.id.titleTextView);
        textView.setText(getArguments().getString("fragmentTitle"));

        ListView listView = (ListView)v.findViewById(R.id.listview);

        arrayListSetup();

        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                getActivity(),
                android.R.layout.simple_list_item_1,
                arrayList
        );

        listView.setAdapter(arrayAdapter);

        return v;
    }

    private void arrayListSetup(){
        StringBuilder stringBuilder = new StringBuilder();
        try {
            BufferedReader br = new BufferedReader(new FileReader(new MainActivity().FILENAME));
            String line;

            // add line to arraylist
            while((line = br.readLine()) != null){
                arrayList.add(line);
            }
            // close bufferedreader
            br.close();

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

The MainActivity when clicking the button, it writes the todo-activity to the file:

addTodoButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // Get category of to-do
                String category = spinner.getSelectedItem().toString();

                // Get the to-do text what has to be done
                String todo_text = editText.getText().toString();


                // Try to create inputfile
                try {
                    FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
                    fos.write(todo_text.getBytes());
                    fos.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        });

I think you should call the function arrayListSetup() in your fragment's onCreateView()
You have declared your arraylist but haven't populated it by calling the arraylistsetup() function, call it in the onCreateView()

问题是我在调用openFileInput()之前没有调用getActivity() openFileInput() ,因为Fragment不是上下文!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM