繁体   English   中英

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

[英]Simple Text Editor does not show the list of notes

我试图运行在这里找到的代码
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);
}
}

将注释设置为适配器后,请准备注释。 我认为这将解决此问题:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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