Implemented Assets File Reader

This commit is contained in:
mmk2410 2015-03-31 00:55:02 +02:00
parent 458aa5e64d
commit 0e6b5e610b
2 changed files with 37 additions and 3 deletions

View file

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

View file

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