简体   繁体   中英

Populate a LinearLayout using an Array of saved files

I'm practicing creating an app that allows users to write notes, store them, and view them as individual icons in a different activity. I have already set up three different activities

  1. MainPage: where the initial activity begins
  2. InputActivity: where the user writes and saves their note
  3. The shelf activity to display the previously written notes

Overall I believe I have completed the tasks that is linked with the initial two activities. I am able to save these files, and have a simple loop to increment the filenames.

What I am having trouble is accessing the saved files in a different activity. Moreover, I have no success in creating an Array from the filenames.

MainPageActivity

public class MainPageActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main_page);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View LoginActivity) {
                Snackbar.make(LoginActivity, "Begin Writing Your Own Diary", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
                launchActivity();
            }
        }
        );
    }

    public void launchActivity(){
        Intent intent = new Intent(this, InputActivity.class);
        startActivity(intent);
    }

    @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_page, menu);
        return true;
    }

    @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);
    }
}

Input Activity

public class InputActivity extends AppCompatActivity {
    private TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_input);
        text = findViewById(R.id.textView);
        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                saveText();
                LaunchActivity();
            }
        });
    }

    public void saveText(){
        int num = 0;
        String fileContents = text.getText().toString();
        FileOutputStream outputStream;
        String fileName = ("");
        if (fileName == fileName) {
            num++;
            fileName = "diary"+num+".txt";
        }
        try {
            outputStream = openFileOutput(fileName, Context.MODE_PRIVATE);
            outputStream.write(fileContents.getBytes());
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void LaunchActivity(){
        Intent intent = new Intent(this, ShelfActivity.class);
        startActivity(intent);
    }
}

ShelfActivity

public class ShelfActivity extends AppCompatActivity {
    File path = getFilesDir();
    File file[]=path.listFiles();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shelf);
        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) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
            }
        });
        loadViews();
    }

    public void loadViews() {
        String[] textArray = file[];
        LinearLayout linLayout = new LinearLayout(this);
        setContentView(linLayout);
        linLayout.setOrientation(LinearLayout.VERTICAL);
        for (int i = 0; i<textArray.length; i++) {
            TextView view = new TextView(this);
            view.setText(textArray[i]);
            linLayout.addView(view);
        }
    }

    public void loadFiles() {

    }
}

As you can see my I am somewhat stuck on my ShelfActivity section. I am not sure why

String[] textArray = file[];

in the method loadViews(), is giving an unexpected token error.

I might have other mistakes and functionality errors that I wasn't able to see. Please point them out so I can learn from my mistakes!

PS, I do have the permission set to:

<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE"/> 

Also, I am fairly new to Android Studio and Java in general, what are some other ways that you think will be easier to accomplish what I am trying to do?

You are trying to convert a File array into a String array, by simply assigning it. That's not going to work. Try something like this instead:

String[] textArray = new String[file.length];
for(int i = 0; i < file.length; i++) {
    textArray[i] = file[i].getName();
}

Get the filename for each file, one by one, and store it in textArray .

Also for future reference, when trying to assign an array, you have to omit the [] .

String[] values = otherArrayOfSameType; // Not otherArrayOfSameType[];

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