Implemented Assets File Reader
This commit is contained in:
parent
458aa5e64d
commit
0e6b5e610b
2 changed files with 37 additions and 3 deletions
1
app/src/main/assets/codes.txt
Normal file
1
app/src/main/assets/codes.txt
Normal 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;
|
|
@ -1,7 +1,6 @@
|
||||||
package de.marcelkapfer.morseconverter;
|
package de.marcelkapfer.morseconverter;
|
||||||
|
|
||||||
import android.app.FragmentManager;
|
import android.content.res.AssetManager;
|
||||||
import android.app.FragmentTransaction;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.v4.app.Fragment;
|
import android.support.v4.app.Fragment;
|
||||||
import android.support.v7.widget.LinearLayoutManager;
|
import android.support.v7.widget.LinearLayoutManager;
|
||||||
|
@ -10,13 +9,19 @@ import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
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 {
|
public class writtenMorseListFragment extends Fragment {
|
||||||
|
|
||||||
private RecyclerView mRecyclerView;
|
private RecyclerView mRecyclerView;
|
||||||
private RecyclerView.Adapter mAdapter;
|
private RecyclerView.Adapter mAdapter;
|
||||||
private RecyclerView.LayoutManager mLayoutManager;
|
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";
|
private static final String TAG = "RecyclerViewFragment";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -32,9 +37,37 @@ public class writtenMorseListFragment extends Fragment {
|
||||||
mRecyclerView.setHasFixedSize(true);
|
mRecyclerView.setHasFixedSize(true);
|
||||||
mLayoutManager = new LinearLayoutManager(getActivity());
|
mLayoutManager = new LinearLayoutManager(getActivity());
|
||||||
mRecyclerView.setLayoutManager(mLayoutManager);
|
mRecyclerView.setLayoutManager(mLayoutManager);
|
||||||
|
getDataset();
|
||||||
mAdapter = new MyAdapter(myDataset);
|
mAdapter = new MyAdapter(myDataset);
|
||||||
mRecyclerView.setAdapter(mAdapter);
|
mRecyclerView.setAdapter(mAdapter);
|
||||||
return rootView;
|
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()]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue