4

I have viewed similar questions on stack and have been unsuccessful finding an answer to my particular bump in the road. I will donate to anyone that can give me a solution.

I am trying to display the contents (.txt files) from a external storage directory without the extensions. I have imported (apache.commons.io).

ArrayAdapter mArrayAdapter;
ListView listView;
String fileNames[];
ArrayList<String> nameArray;



protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    this.setContentView(R.layout.activity_read_note_menu);

    listView = (ListView) this.findViewById(R.id.readListView);
    fileNames = new File(String.valueOf(Environment.getExternalStorageDirectory().getAbsolutePath()) + "/Notes").list();

    nameArray = new ArrayList<>();

         for (String name: fileNames){
            name = name.substring(0, name.indexOf("."));
            nameArray.add(name);
            Log.d("Files", "FileName:" + name);

    }

    mArrayAdapter = new ArrayAdapter<>(this, R.layout.list_item_1, nameArray);
    mArrayAdapter.sort(new Comparator<String>() {

        public int compare(String li1, String li2) {

            return li1.compareTo(li2);
        }

    });

    listView.setAdapter(mArrayAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView adapterView, View view, int i, long l) {
            String string = (String) listView.getItemAtPosition(i);
            Intent intent = new Intent(ReadNoteMenu.this, ReadNote.class);
            intent.putExtra(EXTRA_MESSAGE, string);
            ReadNoteMenu.this.startActivity(intent);

            finish();

        }

    });

1 Answers1

1

Make sure you are getting all file list from your below code:

String fileNames[] = new  File(String.valueOf(Environment.getExternalStorageDirectory().getAbsolutePath()) + "/Notes").list();

once you get all file list, remove the extension from file name and store that name in array.

ArrayList<String> nameArray = new ArrayList<String>();

for (String name: fileNames){
   name = name.substring(0, name.indexOf("."));
   nameArray.add(name);
   Log.d("Files", "FileName:" + name);
}

Please pass nameArray in your adapter to get things done, as below:

mArrayAdapter = new ArrayAdapter(this, R.layout.list_item_1, nameArray);
Uniruddh
  • 4,427
  • 3
  • 52
  • 86
  • Its a step closer to what I'm trying to accomplish. I edited my code according to your advice. The problem is know only one file is showing in the listView. The good news is that one file isn't displaying the extension. It is logging all the files though, just not showing all in the listView. Futher advice?? –  Apr 29 '15 at 07:02
  • The edits are a success! My listView is displaying all files without extensions. I've ran into another problem now with the edited code. When I click on an item (file) in the listView. The file name will show but not the content (text). Any idea?? –  Apr 29 '15 at 08:13
  • To show text, you have to read each text file and show that text in your view. you may ask separate question for that. Afa question is concerned I hope it covers your problem, consider accepting answer if it helps. – Uniruddh Apr 29 '15 at 08:20
  • this can help to read text: `http://stackoverflow.com/questions/12421814/how-can-i-read-a-text-file-in-android` – Uniruddh Apr 29 '15 at 08:23
  • Thanks for everything. As a new coder/dev I run into problems often so every bit of help is appreciated. I can donate via PayPal to show my gratitude. If you will accept email me m4tchb0x87@gmail.com. –  Apr 29 '15 at 08:30
  • You are welcome, and thanks for your kindness. I had also passed form same period of coding. Helping each other, that is what for we are here. :) – Uniruddh Apr 29 '15 at 08:40