2015-02-09 24 views
0

如何優化ListViewAndroid正在填充使用CursorAdapter?我正嘗試創建一個MediaPlayer,該列表隨歌曲標題和專輯封面出現,但列表中的滾動非常緩慢。 如何優化Listrecycle view呢?在Android中使用CursorAdapter優化ListView?

好的,我在這裏添加我的代碼.....所以我讓這個類擴展了CursorAdapter,然後我在類中創建了它的對象,我也想讓它填充它......然後我爲每個類設置一個類型並根據該類型填充特定版本。

public class PopulatingListAdapter extends CursorAdapter { 

private final static int ALL_SONGS_TYPE = 0; 
private final static int ALBUM_SONGS = 1; 
private final static int ARTIST_SONGS = 2; 
private final static int ALBUM_TYPE = 3; 
private final static int ARTIST_TYPE = 4; 

private final LayoutInflater myInflater; 

private int typeOfList; 

public void setType(int type){ 
    typeOfList=type; 
} 

View myView=null; 

public PopulatingListAdapter(Context context, Cursor c, int flags) { 
    super(context, c, flags); 
    // TODO Auto-generated constructor stub 
    myInflater = LayoutInflater.from(context); 
} 

public void initializingVariables(Activity activity){ 

} 


@Override 
public void bindView(View view, Context context, Cursor cursor) { 
    // TODO Auto-generated method stub 

    if(typeOfList==ALL_SONGS_TYPE) 
    { 
     TextView songTitleNameAllSongs = (TextView) view.findViewById(R.id.all_song_title); 
     songTitleNameAllSongs.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE))); 

     TextView songDisplayNameAllSongs = (TextView) view.findViewById(R.id.all_song_display); 
     songDisplayNameAllSongs.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME))); 

     ImageView albumArtInAllSongs = (ImageView) view.findViewById(R.id.album_art_all_songs_single_row); 
     Cursor myCursor; 
     String[] projection = {MediaStore.Audio.Albums._ID, 
           MediaStore.Audio.Albums.ALBUM, 
           MediaStore.Audio.Albums.ALBUM_ART}; 
     myCursor = context.getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, projection, null, null, null); 
     int row=0; 
     if(myCursor.moveToFirst()) 
     { 
      while(row<myCursor.getCount()) 
      { 

      if(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)).equals(myCursor.getString(myCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM)))) 
      { 
      Bitmap bmp = BitmapFactory.decodeFile(myCursor.getString(myCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART))); 
      if(bmp!=null) 
       albumArtInAllSongs.setImageBitmap(bmp); 
      else 
       albumArtInAllSongs.setImageDrawable(context.getResources().getDrawable(R.drawable.head_10)); 
      } 
      myCursor.moveToPosition(++row); 
      } 
     } 

    }else if(typeOfList==ALBUM_TYPE) 
    { 
     TextView albumCoverTitleInAllAlbums = (TextView) view.findViewById(R.id.album_cover_title); 
     albumCoverTitleInAllAlbums.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM))); 

     TextView artistAlbumTitleInAllAlbums = (TextView) view.findViewById(R.id.artist_album_title); 
     artistAlbumTitleInAllAlbums.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ARTIST))); 

     ImageView albumArtInAllAlbum = (ImageView) view.findViewById(R.id.album_single_row_album_art); 
     Bitmap bmp = BitmapFactory.decodeFile(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART))); 
     if(bmp!=null) 
      albumArtInAllAlbum.setImageBitmap(bmp); 
     else 
      albumArtInAllAlbum.setImageDrawable(context.getResources().getDrawable(R.drawable.head_10));   
    }else if(typeOfList==ARTIST_TYPE) 
    { 
     TextView artistNameInAllArtist = (TextView) view.findViewById(R.id.artists_single_name); 
     artistNameInAllArtist.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Artists.ARTIST))); 

    }else if(typeOfList==ALBUM_SONGS) 
    { 
     TextView albumSongTitleNameInAlbumSongs = (TextView) view.findViewById(R.id.albums_songs_name_of_song); 
     albumSongTitleNameInAlbumSongs.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE))); 

     TextView albumSongDisplayNameInAlbumSongs = (TextView) view.findViewById(R.id.albums_songs_name_of_album); 
     albumSongDisplayNameInAlbumSongs.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM))); 
    }else if(typeOfList==ARTIST_SONGS){ 
     TextView artistSongTitleNameInArtistSongs = (TextView) view.findViewById(R.id.artist_song_title_name); 
     artistSongTitleNameInArtistSongs.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE))); 

     TextView artistSongDisplayNameInArtistSongs = (TextView) view.findViewById(R.id.artist_song_artist_name); 
     artistSongDisplayNameInArtistSongs.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST))); 
    } 
    } 


@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 
    // TODO Auto-generated method stub 
     if(typeOfList==ALL_SONGS_TYPE) 
     { 
      myView = myInflater.inflate(R.layout.all_songs_single_row, parent, false); 
     }else if(typeOfList==ALBUM_TYPE) 
     { 
      myView = myInflater.inflate(R.layout.album_single_row, parent, false); 
     }else if(typeOfList==ARTIST_TYPE) 
     { 
      myView = myInflater.inflate(R.layout.artists_single_row, parent, false); 
     }else if(typeOfList==ALBUM_SONGS) 
     { 
      myView = myInflater.inflate(R.layout.albums_songs_single_row, parent, false); 
     }else if(typeOfList==ARTIST_SONGS) 
     { 
      myView = myInflater.inflate(R.layout.artists_song_single_row, parent, false); 
     } 
    return myView; 
} 
+0

你使用ViewBinder ? – pskink 2015-02-09 19:25:05

+0

你是指cursorAdapter中的bindView方法? – 2015-02-09 19:43:01

+0

我們應該看看你的代碼,以瞭解如何對你有所幫助 – 2015-02-09 19:44:37

回答

0

它看起來像你在主(UI)線程上大量下載圖像和音頻文件。爲了提高性能,應該在後臺線程上下載大文件,以便它們不會減慢用戶界面的速度。當後臺線程完成其任務時,您需要一種機制讓他們通知您的活動並傳遞加載的數據。有幾種方法可以做到這一點,但對於這個論壇來說,完整的描述太長了。

下面是一些類的研究,可與後臺加載幫助:
Loader
AsyncTask

請看看這些教程以獲取更多信息:
Processing Bitmaps Off the UI Thread
Loading Data in the Background

+0

我已經使用cursorloader在後臺加載這些數據......而且我也知道在這段代碼中有很多下載,但事情是我無法運行查詢來從不同的位置獲取歌曲和專輯封面在內容提供商提供的數據庫...這就是爲什麼我必須運行一個單獨的查詢來獲取專輯藝術 – 2015-02-10 07:24:54

+0

歌曲的數據庫是媒體和專輯它是專輯...我不能運行這些表在同一個裝載機, t知道如何在同一個類中使用多個加載器 – 2015-02-10 07:26:04

+0

從CursorLoader獲取初始光標後,必須加載大型媒體文件是很常見的。現在看起來你的初始光標只返回到你的大媒體的文本和URI鏈接,這是你想要的。現在在您的CursorAdapter.bindView()方法中,不是直接在UI線程上運行URI鏈接查詢,而是在AsyncTask或不同的Loader上運行它們,並在它們完成時爲它們提供回調以填充它們的部分。原帖中提供的教程是一個很好的開始。 – happydude 2015-02-11 04:07:36