0
Reply

Alertdialog builder with text file reader

JOHN JOHNNNY

JOHN JOHNNNY

Aug 19 2015 12:33 PM
573
I have a listview, when clicked opens a dialog and i also have
a code that reads text file from assets/raw folder.
What i really want to achieve is to read each text files in dialog
i.e whenever a user clicks on list item it opens a text files for each
listview item. NB am having 10 different text files in my assets/raw folder
How can i combine both codes together to achieve this
See the code below
Activity with listviews and dialog
public class KneelingDown extends ActionBarActivity {
// List view
private ListView lv;
private Toolbar toolbar;
// Listview Adapter
ArrayAdapter<String> adapter;
private ListView listView;
private MyAdapter myAdapter;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kneeling_down);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
//Part of cardview code starts here
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerList);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(llm);
recyclerView.setAdapter(new MyRecyclerAdapter(generatePalettes()));
recyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(context, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
switch (position) {
case 0:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
KneelingDown.this);
// set title
alertDialogBuilder.setTitle("Yah Rah man Hi");
alertDialogBuilder.setIcon(R.mipmap.ic_launcher);
// set dialog message
alertDialogBuilder
.setMessage("Text file content here ")
.setCancelable(false)
.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close
// current activity
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
break;
case 1:
AlertDialog.Builder alertDialogBuilder1 = new AlertDialog.Builder(
KneelingDown.this);
// set title
alertDialogBuilder1.setTitle("O Christ Oh my king");
alertDialogBuilder1.setIcon(R.mipmap.ic_launcher);
// set dialog message
alertDialogBuilder1
.setMessage("Text file content here ")
.setCancelable(false)
.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close
// current activity
}
});
// create alert dialog
AlertDialog alertDialog1 = alertDialogBuilder1.create();
// show it
alertDialog1.show();
break;
}
}
})
);
//Ends here
}
//Remaining part of CardView Starts Here
private ArrayList<Palette> generatePalettes() {
ArrayList<Palette> palettes = new ArrayList<Palette>();
palettes.add(new Palette("Hymn 003 Yah Rah man Hi", R.mipmap.ic_launcher));
palettes.add(new Palette("Hymn 004 O Christ Oh my king", R.mipmap.ic_launcher));
return palettes;
}
Activity with reading text files
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = (TextView)findViewById(R.id.textview_data);
String data = readTextFile(this, R.raw.fixures);
textView.setText(data);
}
public static String readTextFile(Context ctx, int resId)
{
InputStream inputStream = ctx.getResources().openRawResource(resId);
InputStreamReader inputreader = new InputStreamReader(inputStream);
BufferedReader bufferedreader = new BufferedReader(inputreader);
String line;
StringBuilder stringBuilder = new StringBuilder();
try
{
while (( line = bufferedreader.readLine()) != null)
{
stringBuilder.append(line);
stringBuilder.append('\n');
}
}
catch (IOException e)
{
return null;
}
return stringBuilder.toString();
}
Kindly help