Search code examples
androidxmllistviewadapter

Addidng data to ListView by the own adapter


I need to add data to ListView by adapter. Adapter has some data. enter image description here As you can see here, the size of ArrayList is 417.But when I open the activity where the ListView is, I don't see anything here. Here's the code of adapter:

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;
}

Code of Song class:

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>

Layout of ativity, where there is a 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>

Solution

  • See this you are wrong here

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

    Make it

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

    Remove these two methods also:

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

    Explanation Adapter uses getCount() method to identify the size of List and recyclerview you are returning 0 here means you have 0 items in listview. What actually we need is return the total items size which is songs.size().

    Hope this will give you a proper guidence.