如何设置两个适配器插入一个列表?适配器、如何设置、两个、列表

2023-09-06 18:17:31 作者:朢珎銨寧

我在android系统工作。我创建一个音乐应用程序。在这个应用程序,我想展示的艺术家的歌曲和名称的标题到列表中。    这是我的code: -

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**

这是创建问题名单无法setAdapter两次。所以请告诉我解决它,所以我可以将 setAdapter 作为歌曲及歌手名称的两个称号。             这是创建问题名单无法setAdapter两次。所以请告诉我解决它,所以我可以将 setAdapter 作为歌曲及歌手名称的两个称号。

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.

感谢您提前。

推荐答案

我不知道什么是你想要的目的。无论你的适配器具有相同的codeS。为什么不创建一个具有两个适配器的值的单个适配器。

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.

刚才看到您的编辑问题

创建定制适配器和膨胀具有两个textviews的布局。 - 一个用于艺术家和一个用于歌曲

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;     

}