简单文本编辑器不显示注释列表

时间:2017-08-21 13:21:09

标签: java android notepad

我试图运行我在这里找到的代码  http://www.androidauthority.com/lets-build-a-simple-text-editor-for-android-773774/ 我的问题是,当我添加一个新笔记时,它会被保存,但是当我尝试显示已保存笔记的列表时,它无法正常工作

这是代码:

public class Select extends AppCompatActivity {

private List<NoteBuilder> notesList = new ArrayList<>();
private NoteAdapter nAdapter;
private RecyclerView notesRecycler;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_select);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent newNote = new Intent(Select.this, MainActivity.class);
            Select.this.startActivity(newNote);
        }
    });

    notesRecycler = (RecyclerView) findViewById(R.id.notes);

    nAdapter = new NoteAdapter(notesList);
    RecyclerView.LayoutManager mLayoutManager =
            new LinearLayoutManager(getApplicationContext());
    notesRecycler.setLayoutManager(mLayoutManager);
    notesRecycler.setItemAnimator(new DefaultItemAnimator());
    notesRecycler.setAdapter(nAdapter);

    prepareNotes();

}

private void prepareNotes() {
    File directory;
    directory = getFilesDir();
    File[] files = directory.listFiles();
    String theFile;
    for (int f = 1; f >= files.length;
         f++) {
        theFile = "Note" + f + ".txt";
        NoteBuilder note = new NoteBuilder(theFile, Open(theFile));
        notesList.add(note);
    }

}

public String Open(String fileName) {
    String content = "";
    try {
        InputStream in = openFileInput(fileName);
        if (in != null) {
            InputStreamReader tmp = new InputStreamReader(in);
            BufferedReader reader = new BufferedReader(tmp);
            String str;
            StringBuilder buf = new StringBuilder();
            while ((str = reader.readLine()) != null) {
                buf.append(str + "\n");
            }
            in.close();

            content = buf.toString();
        }
    } catch (java.io.FileNotFoundException e) {
    } catch (Throwable t) {
        Toast.makeText(this, "Exception: " + t.toString(), Toast.LENGTH_LONG).show();
    }

    return content;
}
}

我很困惑。这可能是什么问题?

保存笔记的代码:

public class MainActivity extends AppCompatActivity {
EditText EditText1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Save("Note1.txt");
        }
    });

    EditText1 = (EditText) findViewById(R.id.EditText1);
    EditText1.setText(Open("Note1.txt"));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

public void Save(String fileName) {
    try {
        OutputStreamWriter out =
            new OutputStreamWriter(openFileOutput(fileName, 0));
        out.write(EditText1.getText().toString());
        out.close();
        Toast.makeText(this, "Note saved!", Toast.LENGTH_SHORT).show();
    } catch (Throwable t) {
        Toast.makeText(this, "Exception: " + t.toString(), Toast.LENGTH_LONG).show();
    }
}

public String Open(String fileName) {
    String content = "";
    if (FileExists(fileName)) {
        try {
            InputStream in = openFileInput(fileName);
            if ( in != null) {
                InputStreamReader tmp = new InputStreamReader( in );
                BufferedReader reader = new BufferedReader(tmp);
                String str;
                StringBuilder buf = new StringBuilder();
                while ((str = reader.readLine()) != null) {
                    buf.append(str + "\n");
                } in .close();
                content = buf.toString();
            }
        } catch (java.io.FileNotFoundException e) {} catch (Throwable t) {
            Toast.makeText(this, "Exception: " + t.toString(), Toast.LENGTH_LONG).show();
        }
    }
    return content;
}

public boolean FileExists(String fname) {
    File file = getBaseContext().getFileStreamPath(fname);
    return file.exists();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

1 个答案:

答案 0 :(得分:0)

设置到适配器后准备笔记。我想这会解决它:

prepareNotes(); //do this first? <<<<<<<
 notesRecycler = (RecyclerView) findViewById(R.id.notes);

nAdapter = new NoteAdapter(notesList);
RecyclerView.LayoutManager mLayoutManager =
        new LinearLayoutManager(getApplicationContext());
.....