通过自己的适配器将数据添加到ListView

时间:2019-01-15 09:35:49

标签: android xml listview adapter

我需要通过适配器将数据添加到ListView。适配器有一些数据。 only constructor 如您所见,ArrayList的大小为417.但是当我打开ListView所在的活动时,在这里看不到任何内容。这是适配器的代码:

package asus.example.com.player;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

public class SongAdapter extends BaseAdapter {
    private ArrayList<Song> songs;
    private LayoutInflater layoutInflater;

    SongAdapter(Context context, ArrayList<Song> songs){
        this.songs = songs;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {

    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    @SuppressLint("ViewHolder")
    LinearLayout songLay = (LinearLayout) layoutInflater.inflate(R.layout.song, parent, false);
    TextView songTitle = songLay.findViewById(R.id.songTitle);
    TextView songArtist = songLay.findViewById(R.id.songArtist);
    Song curSong = songs.get(position);
    songTitle.setText(curSong.getTitle());
    songArtist.setText(curSong.getArtist());
    songLay.setTag(position);
    return songLay;
}

歌曲类代码:

package asus.example.com.player;

public class Song {
    private long id;
    private String title;
    private String artist;

    public long getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    String getArtist() {
        return artist;
    }

    Song(long id, String title, String artist){
        this.id = id;
        this.title = title;
        this.artist = artist;
    }
}

Song.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/songTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#1111ff"
        android:textSize="20sp"
        android:textStyle="bold"/>

    <TextView
        android:id="@+id/songArtist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#2a7800"
        android:textSize="18sp" />

</LinearLayout>

无穷大布局,其中有一个ListView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ListOfSongsActivity">

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:ignore="UselessParent">

        <ListView
            android:id="@+id/songList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:ignore="NestedScrolling" />

    </ScrollView>
</LinearLayout>

2 个答案:

答案 0 :(得分:1)

看到这里您错了

@Override
    public int getCount() {
        return 0;
    }

制作

 @Override
    public int getCount() {
        return songs.size();
    }

也删除这两种方法:

 @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {

    return 0;
}

说明适配器使用getCount()方法来标识要返回0的List和recyclerview的大小,这里意味着0在listview中。我们实际需要的是返回商品总大小songs.size()

希望这会为您提供适当的指导。

答案 1 :(得分:1)

对适配器类进行以下更改,然后检查一次

@Override
public int getCount() {
    //return 0;
    return songs.size();
}