1

I am working in android. I am creating a music application. in this application i want to show title of song and name of artist into list. this is my code :-

 String[] title_of_song={"title1","title2","title3","title4","title5"};
 String[] artist_of_song={"artist1","artist2","artist3","artist4","artist5"};
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.row_button, R.id.TextView_songName, title_of_song);


 ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, R.layout.row_button, R.id.TextView_byArtist, artist_of_song);

 list.setAdapter(adapter);
 list.setAdapter(adapter1); //**this is creating error**

this is creating problem that list is unable to setAdapter two times. so please tell me solution for it so i can set setAdapter for both title of song and artist name. this is creating problem that list is unable to setAdapter two times. so please tell me solution for it so i can set setAdapter for both title of song and artist name.

Thank you in advance.

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
Pushpendra Kuntal
  • 6,118
  • 20
  • 69
  • 119
  • title1 performer is artist1 and so on ? in othr words is there a conection between title and artist ? – Selvin Sep 15 '11 at 10:42
  • yes sir both are related. means we have two arrays for each song, one array holds title of song and second array has artist name for that song. In other words if i use title_of_song[1],and artist_of_song[1] then these are information for song 1. means both are related to each other. – Pushpendra Kuntal Sep 15 '11 at 10:45
  • ok so when you want to display it. alternatively? – user370305 Sep 15 '11 at 10:47
  • so Umesh gave you a good answer ... you need to use Adapter derived from ArrayAdapter where `class Song { public String title; public String artist; }` or use SimpleAdapter and store data in List> where sigle song is instance of HashMap like this {"title": "title1", "artist" : "artist1"} see this answer for more details http://stackoverflow.com/questions/6305899/custom-listview-android/6306901#6306901 – Selvin Sep 15 '11 at 10:51
  • What you want? you want to display two list alternatively on some condition in activity or want to display to different string array in one list? – user370305 Sep 15 '11 at 10:53

3 Answers3

1

I suggest adding title - artist into one string or in any xml and using single adapter it gives your code more flexibility and increase the reusability of your code.

Love Garg
  • 406
  • 1
  • 5
  • 17
1

I do not know what are you trying to achieve. both your adapters have identical codes. why not create a single adapter that has values of both adapters.

Just saw your edited question

create a custom adapter and inflate a layout which has two textviews - one for artist and one for the song.

class TaskAdapter extends ArrayAdapter{

    ArrayList artist, song;
    public TaskAdapter(ArrayList songs, ArrayList artist){
           this.artist = artist;
           this.song = songs;
    }       


    public TaskAdapter(Context context, int textViewResourceId ) {
        super(context, textViewResourceId);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;
        ViewHolder holder;
        if(v == null) {
            LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = li.inflate(R.layout.custom_list, null);
            holder = new ViewHolder();
            holder.taskId = (TextView)v.findViewById(R.id....);
            holder.taskDesc = (TextView)v.findViewById(R.id....);

            v.setTag(holder); 
        }
        else{
            holder = (ViewHolder)v.getTag();
        }

        holder.tArtist.setText(artist.get(position));

        holder.tSong.setText(song.get(position));           


        return v;
    }       

}   
static class ViewHolder {
    TextView tArtist;
    TextView tSong;     

}
Umesh
  • 4,406
  • 2
  • 25
  • 37
0

I suggest adding title - artist into one string and using single adapter or use two separate lists.

Rahul Choudhary
  • 3,789
  • 2
  • 30
  • 30