Justin D
Justin D

Reputation: 697

Customized Listview contains individual buttons in android

I am developing an android app that needs a customized listview.

I need a customized listview that looks like:

enter image description here

Whenever I click to Speaker button, it plays the content of the item it belongs to.

I don't know how many item the listview will has, so I wonder how to name all speaker-buttons and how to get the corresponding content of any button!

Any idea?

Thanks!

Upvotes: 0

Views: 415

Answers (3)

Paresh Mayani
Paresh Mayani

Reputation: 128428

You need to create a custom adapter class by extending BaseAdapter or ArrayAdapter class.

After creating a custom adapter class, override getView() method.

You can check below links:

Or you can check my blog for the ListView category.

Upvotes: 1

Jayeshkumar Sojitra
Jayeshkumar Sojitra

Reputation: 2511

You, can do using following method too.

First of all define list view and list of music array which you want

List<HashMap<String, String>> MusicArray;
ListView MusicList;

Now initialize both MusicArray and MusicList both, and assign values of song list into MusicArray.

Now create custom cell layout using xml file

Then Create custom class ViewHolder which will layout the xml file.

class ViewHolder {

    TextView SongName, SongDescription;
    int id;
    }

Then now create Custom_Music_List which will extend BaseAdapter as given below.

public class Custom_Music_List extends BaseAdapter {
    Context contex;
    ViewHolder holder;

    List<HashMap<String, String>> MusicItems;
    TextView SongName, SongDescription;

    public Custom_Music_List(Context context,
            List<HashMap<String, String>> MusicArray) {

        contex = context;
        MusicItems = MusicArray;

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return MusicItems.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView,
            ViewGroup parent) {
        // TODO Auto-generated method stub

        if (convertView == null) {

            holder = new ViewHolder();

            LayoutInflater inflater = (LayoutInflater) LayoutInflater
                    .from(contex);
            convertView = inflater.inflate(R.layout.custom_music_cell, parent,
                    false);

            holder.SongName = (TextView) convertView.findViewById(R.id.SongName);
            holder.SongName.setText(MusicItems.get(position).get("SongName"));

            holder.SongDescription = (TextView) convertView.findViewById(R.id.SongDescription);
            holder.SongDescription.setText( + MusicItems.get(position).get("SongDescription"));

        }

        return convertView;
    }

}

Then set adapter to MusicList by given following way and you will be able to get the appropriate list item which is clicked.

MusicList.setAdapter(new Custom_contact(ClassName.this, MusicArray));
MusicList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View view,
                int position, long arg3) {

            // You will get position of row clicked from here using position, then try to access particular item from list using position

            Log.i("Song Clicked", MusicArray.get(position).get("SongName"))

            // Perform Action based upon position of song

        }
});

Upvotes: 1

Harshid Vasoya
Harshid Vasoya

Reputation: 5721

As per @Paresh Mayani says you have to create custom Adapter.

You can find it out demo or reference link from here.

you can implement as per your requirement if you are finding any trouble then let me know.

Upvotes: 0

Related Questions