2016-09-21 31 views
0

我正在製作一個應用程序,它將從SD卡中獲取歌曲,並在列表視圖中填充 。但由於某些原因,它不會填充並顯示空白片段。你可以請幫忙,可能是造成這個問題的原因是 。謝謝。Listview不填充從SD卡片段歌曲

bellasongs.java

public class BellaSongs extends ListFragment { 

    private ArrayList<ModelBellaSongs> BellaSongList; 
    private ListView Bellasongviews; 
    Context context; 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootview = inflater.inflate(R.layout.bella_songs, container, false); 
     Bellasongviews = (ListView)rootview.findViewById(android.R.id.list); 

     BellaSongList = new ArrayList<ModelBellaSongs>(); 
     return rootview; 
    } 

    public void getBellaSongList() { 
     ContentResolver musicResolver = context.getContentResolver(); 
     Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 
     Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null); 

     if (musicCursor != null && musicCursor.moveToFirst()) { 
      int titlecolumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.TITLE); 
      int idcolumn = musicCursor.getColumnIndex(MediaStore.Audio.Media._ID); 
      int artistcolumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST); 

      do { 
       long thisId = musicCursor.getLong(idcolumn); 
       String thistitle = musicCursor.getString(titlecolumn); 
       String thisartist = musicCursor.getString(artistcolumn); 
       BellaSongList.add(new ModelBellaSongs(thisId, thistitle, thisartist)); 
      } while (musicCursor.moveToNext()); 
     } 
     getBellaSongList(); 

     Collections.sort(BellaSongList, new Comparator<ModelBellaSongs>() { 
      @Override 
      public int compare(ModelBellaSongs a, ModelBellaSongs b) { 
       return a.getTitle().compareTo(b.getTitle()); 
      } 
     }); 

     BellaSongsAdapter bellasongsadapter = new BellaSongsAdapter(getActivity(),BellaSongList); 
      Bellasongviews.setAdapter(bellasongsadapter); 
    } 
} 

adapter.java

<?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"> 


<ListView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@android:id/list" 
    android:layout_gravity="center_vertical" 
    android:layout_weight="1" /> 

</LinearLayout> 

bellasongsadapter.java

public class BellaSongsAdapter extends BaseAdapter { 

    private ArrayList<ModelBellaSongs> bellasongs; 
    private LayoutInflater songdetails; 

    public BellaSongsAdapter(Context c, ArrayList<ModelBellaSongs>thesongs){ 
     bellasongs = thesongs; 
     songdetails = LayoutInflater.from(c); 
    } 

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

    @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) { 
     LinearLayout songLayout=(LinearLayout)songdetails.inflate(R.layout.singlelist_item,parent,false); 
     TextView titleview=(TextView)songLayout.findViewById(R.id.text_song_name); 
     TextView artistview=(TextView)songLayout.findViewById(R.id.text_artist_name); 

     ModelBellaSongs currentsongs = bellasongs.get(position); 
     titleview.setText(currentsongs.getTitle()); 
     artistview.setText(currentsongs.getArtist()); 
     songLayout.setTag(position); 

     return songLayout; 
    } 
} 
+0

返回的遊標不是空的?請附上BellaSongsAdapter.class –

+0

@YuriiTsap這裏我添加了適配器類。這是對的嗎。 – Sam

回答

1

我覺得你的ID refrence是不正確的

Bellasongviews = (ListView)rootview.findViewById(android.R.id.list); 

確保您的這個android.R.id.list ID是正確的,應該從R.id.yourId

不要進口android.R從這裏開始導入您的項目R這裏。

檢查您的XML佈局文件的ID或粘貼它也在這裏在您的問題。

+0

早些時候,我使用R.id.customid,但在運行時它引發了一個異常,該id應該包括android之後的R.id.customid。 – Sam

+0

我已經添加了xml。我已經將android.R.id.list中的id更改爲songs.java中的R.id.list,但最終將xml從android:id/list更改爲了@ @ + id/list,但仍然它不會在運行時填充列表視圖。 – Sam

1

翻看你的代碼,我看到了方法getBellaSingList()。這種方法是檢索數據,甚至自己調用自身,但它不在onCreateView()內調用。所以在技術上 - 你沒有填充你的數據集。您需要在初始化視圖時調用它 - 並請驗證此方法的邏輯。

+0

感謝您的寶貴迴應。我會考慮。我有點不確定'onActivityCreated'和'onViewCreated'之間應該使用哪種方法。根據你我應該考慮什麼。 – Sam

+0

我更喜歡做onActivityCreated() –