From 0e6b5e610bd3099bf89b20fd8c0304b860101e04 Mon Sep 17 00:00:00 2001 From: mmk2410 Date: Tue, 31 Mar 2015 00:55:02 +0200 Subject: [PATCH] Implemented Assets File Reader --- app/src/main/assets/codes.txt | 1 + .../writtenMorseListFragment.java | 39 +++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 app/src/main/assets/codes.txt diff --git a/app/src/main/assets/codes.txt b/app/src/main/assets/codes.txt new file mode 100644 index 0000000..668b445 --- /dev/null +++ b/app/src/main/assets/codes.txt @@ -0,0 +1 @@ +A;B;C;D;E;F;G;H;I;J;K;L;M;N;O;P;Q;R;S;T;U;V;W;X;Y;Z;0;1;2;3;4;5;6;7;8;9; \ No newline at end of file diff --git a/app/src/main/java/de/marcelkapfer/morseconverter/writtenMorseListFragment.java b/app/src/main/java/de/marcelkapfer/morseconverter/writtenMorseListFragment.java index eb58dd9..972b9e0 100644 --- a/app/src/main/java/de/marcelkapfer/morseconverter/writtenMorseListFragment.java +++ b/app/src/main/java/de/marcelkapfer/morseconverter/writtenMorseListFragment.java @@ -1,7 +1,6 @@ package de.marcelkapfer.morseconverter; -import android.app.FragmentManager; -import android.app.FragmentTransaction; +import android.content.res.AssetManager; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v7.widget.LinearLayoutManager; @@ -10,13 +9,19 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.ArrayList; + public class writtenMorseListFragment extends Fragment { private RecyclerView mRecyclerView; private RecyclerView.Adapter mAdapter; private RecyclerView.LayoutManager mLayoutManager; - private String[] myDataset = {"test1","test2","test3","test1","test2","test3","test1","test2","test3","test1","test2","test3"}; + private String[] myDataset; private static final String TAG = "RecyclerViewFragment"; @Override @@ -32,9 +37,37 @@ public class writtenMorseListFragment extends Fragment { mRecyclerView.setHasFixedSize(true); mLayoutManager = new LinearLayoutManager(getActivity()); mRecyclerView.setLayoutManager(mLayoutManager); + getDataset(); mAdapter = new MyAdapter(myDataset); mRecyclerView.setAdapter(mAdapter); return rootView; } + private void getDataset(){ + ArrayList dataset = new ArrayList<>(); + String datastring = ""; + AssetManager astmgr = getActivity().getAssets(); + try { + InputStream data = astmgr.open("codes.txt"); + StringBuilder buf = new StringBuilder(); + BufferedReader in = + new BufferedReader(new InputStreamReader(data, "UTF-8")); + String str; + while ((str = in.readLine()) != null) { + buf.append(str); + } + in.close(); + datastring =buf.toString(); + } catch (IOException e) { + e.printStackTrace(); + } + + for(int c = 0; datastring.length() > 1; c++){ + int pos = datastring.indexOf(";"); + dataset.add(datastring.substring(0, pos)); + datastring = datastring.substring(pos + 1); + myDataset = dataset.toArray(new String[dataset.size()]); + } + } + }