Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Java
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
Java
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
Posted
Updated 12-Aug-15 8:29am
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900