sujanko sujanko - 1 month ago 9
Android Question

Android Songs lyrics app with XML db

Let me first describe my intention. I have a XML DB with some folklore songs and lyrics. For my family and friends I want to develop an android app that will list the songs in A-Z order and after clicking/touching the song title a text/lyrics will appear (so that we can all sing the same words).

I am not very familiar with Java and Android development, nevertheless I managed to create an App where in the first

Activity
there is a button "Show all songs" and after pressing it another
Activity
with
ListView
lists the songs (using
XMLPullHandler
parser).
Collections.sort()
takes care for ordering (although not 100%, because items which start with national characters are pushed to the end). See the screenshot please:

Screenshot of current functionality

But now I am uncertain how to proceed with the display of the song-text. My ideas:


  1. To achieve, that after clicking an item a new
    SongActivity
    will appear and will show the lyrics. But then I should create e.g. 100 intents and activities or can I do it with just one? In the later case - how will the app know which text belongs to the clicked song?

  2. To load the song lyrics text together with the song title and show it in the
    ListView
    , but with an attribute
    invisible
    (in html it is clear, but in java - I am not sure...)



Example of the song in the xml file:

<songs>
<song>
<id>1</id>
<title>Zvalila sa skala</title>
<lyrics>1. [:Zvalila sa skala z vŕšku do Dunaja,:]
[:prenes ma, má milá, prenes ma, má milá, veď je voda malá.:]

2. [:Ja bych ťa preniesla, hoc by bola väčšia,:]
[:ale že som ti nie, ale že som ti nie, tvoja najmilejšia.:]

3. [:Keby ja vedela, kde môj milý býva,:]
[:zaletela by som, zaletela by som, na jeho biely dom.:]
</lyrics>
<type>Valčík</type>
</song> ...


Could someone please give me a hand and guidance in this? I would be most grateful.

On the picture is the actual status with visible values of variables

Answer

To achieve, that after clicking an item a new Song Activity will appear and will show the lyrics. But then I should create e.g. 100 intents and activities, or can I do it with just one? In the later case - how will the app know which text belongs to the clicked song?

No you don't have to write 100 intents or activities. You can use just one which is more generalized and convenient. You can see this SO Documentation for passing data between activities.

To load the song lyrics text together with the song title and show it in the listview, but with an attribute "invisible" (in html it is clear, but in java - I am not sure...)

Though your question is not very clear, I guess you need to show the title of the song along with some text as a secondary text. You're trying to use invisible attribute gives me sense of hiding the secondary text and once the list item is clicked make the secondary text visible - is that it??

If I got you correctly, then you need to modify the adapter you're using for your RecyclerView. You need to customize your adapter and while binding each element of your list, you need to show/hide the secondary text by yourself by tracking which item is selected.

I would suggest you to use RecyclerView to show the list of songs in a list. You'll find how to setup a RecyclerView here. To implement a RecyclerView with a custom adapter you'll find plenty of documentation over the internet. Here I'm attaching one example.

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    // Define the ViewHolder for list item
    public class NormalViewHolder extends ViewHolder {
        public NormalViewHolder(View itemView) {
            super(itemView);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // Do whatever you want on clicking the list items 
                }
            });
        }
    }

    // And now in onCreateViewHolder you have to pass the correct view
    // while populating the list item.

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v;

        v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_normal, parent, false);

        NormalViewHolder vh = new NormalViewHolder(v);

        return vh;
    }

    // Now bind the viewholders in onBindViewHolder
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        try {
            if (holder instanceof NormalViewHolder) {
                NormalViewHolder vh = (NormalViewHolder) holder;

                vh.bindView(position);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        if (data == null) {
            return 0;
        }

        // data is the ArrayList of items you want to show in your list
        return data.size();
    }

    // Now define getItemViewType of your own. 

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    // Now set the default ViewHolder for NormalViewHolder
    public class ViewHolder extends RecyclerView.ViewHolder {
        // Define elements of a row here
        public ViewHolder(View itemView) {
            super(itemView);
            // Find view by ID and initialize here
        }

        public void bindView(int position) {
            // bindView() method to implement actions
        }
    }
}
Comments