2015-02-09 87 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

相关问题