andorid - 如果适配器中没有对象,则显示不同的布局

时间:2016-04-04 16:19:03

标签: android android-layout android-adapter

我正在尝试显示SD卡上的所有歌曲。如果至少有一首歌,代码工作正常,但如果没有歌曲,我会得到一个NullPointerException,所以我想用新的布局替换listview,以防没有像“对不起,没有什么要在这里显示”这样的歌曲< / p>

我尝试了几种方法,但我总是在我的适配器的方法getCount()上得到NullPointerException

所以......这是我的代码:

ListFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    mInflater = inflater;
    rootView   =  inflater.inflate(R.layout.fragment_list, container, false);
    mContext = container.getContext();
    mContainer = container;

    mAdapterListFile = new SongsAdapter(mContext, mSongList);
    mListSongs = (ListView) rootView.findViewById(R.id.listView_songs);
    mListSongs.setAdapter(mAdapterListFile);

    mSongList = new ArrayList<Song>();
    mSongList = listAllSongs();  //there will be added all songs found in SD card        
    return rootView;
}
private ArrayList<Song> listAllSongs() {
    Cursor cursor;
    ArrayList<Song> songList = new ArrayList<Song>();

    Uri allSongsUri  = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
    String orderBy   = MediaStore.Audio.Media.TITLE + " ASC";
    if (isSdPresent()) {

        cursor = getActivity().getContentResolver().query(allSongsUri, STAR, selection, null, orderBy);

        if (cursor != null) {
            if (cursor.moveToFirst()) {
                do {
                    Song song = new Song();

                    String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                    String[] res = data.split("\\.");
                    song.setSongName(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));        //song name                        song.setSongFullPath(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)));     //directory
                    song.setSongId(cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media._ID)));               //id song                       
song.setSongAlbumName(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)));   //album 

song.setSongArtistName(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST))); //artist                    song.setAlbumImage(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART))); 


                    song.setSongUri(ContentUris.withAppendedId(
                            android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                            cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media._ID))));
                    String duration = getDuration(Integer.parseInt(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION))));  //duration
                    song.setSongDuration(duration);
                    songList.add(song);
                                        } while (cursor.moveToNext());
                return songList;
            }
            cursor.close();
        }
    }        return null;
}
private static boolean isSdPresent() {
    return android.os.Environment.getExternalStorageState()
                            .equals(android.os.Environment.MEDIA_MOUNTED);
}

SongsAdapter.java

public class SongsAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<Song> songList;//Data Source for ListView

public SongsAdapter(Context context, ArrayList<Song> list) {

    this.mContext = context;
    this.songList = list;
}

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

@Override
public Song getItem(int position) {
    return songList.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        //Layout inflate for list item
        convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, null);
    }

    ImageView mImgSong = (ImageView) convertView.findViewById(R.id.copertina_album);
    TextView mTxtSongName = (TextView) convertView.findViewById(R.id.titolo_brano);
    TextView mTxtArtista = (TextView) convertView.findViewById(R.id.artista);
    TextView mTxtSongDuration = (TextView) convertView.findViewById(R.id.durata);

    mImgSong.setImageResource(R.drawable.vector_vinyl);
    mTxtSongName.setText(songList.get(position).getSongName());
    mTxtArtista.setText(songList.get(position).getArtistName());
    mTxtSongDuration.setText(songList.get(position).getSongDuration());


    return convertView;
}

public void setSongsList(ArrayList<Song> list) {
    songList = list;
    this.notifyDataSetChanged();
       }
}

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

ListView具有setEmptyView方法,您可以在其中指定在列表为空时显示的视图。

在这里查看: http://developer.android.com/guide/topics/ui/layout/listview.html

答案 1 :(得分:0)

首先在getItemCount()中你应该做这样的检查:

@Override
public int getCount() {
if(songList== null)
        return 0;
    return songList.size();
}

getView()

中的第二个
public View getView(int position, View convertView, ViewGroup parent) {
// check if song list is null and count is null than inlate the view you want to 
// show somethig like this
if(songList== null || songList == 0){
convertView = LayoutInflater.from(mContext).inflate(R.layout.empty_state_view, null);
return convertview;
}
else{
    //your code
}
}