- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在开发音乐播放器。所以我需要在一个自定义 ListView 中加载歌曲列表,它有一个 ImageView 和两个 textView。它的性能太慢了,所以我在谷歌中搜索并找到了一个解决方案,上面写着“使用 AsyncTasks 并在后台加载 listView”。但是listview的性能还是太慢了。请帮助我提高 ListView 的性能。这是我的代码:
此代码通过从 song_database 获取值来加载自定义 ArrayList,它位于 Activity 类中。
private ArrayList getListData() {
ArrayList results = new ArrayList();
Song Data = new Song();
Database_Song db = new Database_Song(this);
String data = db.getData();
StringTokenizer token = new StringTokenizer(data,";");
while(token.hasMoreTokens())
{
String dataPart = token.nextToken();
StringTokenizer tokenPart = new StringTokenizer(dataPart,"|");
Data = new Song();
Data.setSongTitle(tokenPart.nextToken());
mediaInfo.setDataSource(tokenPart.nextToken());
if (mediaInfo.getEmbeddedPicture() != null){
byte[] img = mediaInfo.getEmbeddedPicture();
Data.setImage((BitmapFactory.decodeByteArray(img, 0,img.length)));
}
else
Data.setImage(BitmapFactory.decodeResource(this.getResources(), R.drawable.album_art));
Data.setAlbumName(tokenPart.nextToken());
results.add(Data);
}
return results;
}
这是我的 AsyncTasks 类,它是 Activity 类的内部类。
private class LoadListTask extends AsyncTask<String, Void, Integer> {
ArrayList Song_details;
protected void onPreExecute() {
}
protected Integer doInBackground(String... params) {
Song_details = getListData();
return 0;
}
protected void onPostExecute(Integer result) {
adapter = new CustomListAdapter_Song(cont, Song_details);
lv_song.setAdapter(adapter);
if (result == 0) {
adapter.notifyDataSetChanged();
}
}
}
这是我的适配器类
public class CustomListAdapter_Song extends BaseAdapter{
private ArrayList<Song> listData;
private LayoutInflater layoutInflater;
private ArrayList<Song> arraylist;
private int lastPosition = -1;
public CustomListAdapter_Song(Context context, ArrayList<Song> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
this.arraylist = new ArrayList<Song>();
this.arraylist.addAll(listData);
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.listview_item_row_for_data, null);
holder = new ViewHolder();
holder.songName = (TextView) convertView.findViewById(R.id.Song_songTitle);
holder.albumName = (TextView) convertView.findViewById(R.id.Song_AlbumName);
holder.songImage = (ImageView) convertView.findViewById(R.id.Song_AlbumImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.songName.setText(listData.get(position).getSongTitle());
holder.albumName.setText(listData.get(position).getAlbumName());
holder.songImage.setImageBitmap((listData.get(position).getImage()));
View view = convertView;
Animation animation = AnimationUtils.loadAnimation(view.getContext(), (position > lastPosition) ? R.anim.up_from_bottom : R.anim.down_from_top);
view.startAnimation(animation);
lastPosition = position;
return convertView;
}
static class ViewHolder {
TextView songName;
TextView albumName;
ImageView songImage;
}
最佳答案
不要使用 AsyncTask 但你需要 CursorLoader
public class SongsFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor>
{
SongAdapter adapter;
@Override
public View onCreateView( LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState )
{
return inflater.inflate(R.layout.fragment_songs, container, false);
}
@Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate(savedInstanceState);
adapter = new SongAdapter(getActivity(), null);
setListAdapter(adapter);
getLoaderManager().initLoader(0, null, this);
}
private class SongAdapter extends CursorAdapter
{
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
public SongAdapter( Context context , Cursor c )
{
super(context , c , true);
}
@Override
public View getView( int position , View convertView , ViewGroup parent )
{
View v;
Cursor cursor = (Cursor) getItem(position);
if (convertView == null)
{
v = newView(mContext, cursor, parent);
}
else
{
v = convertView;
}
bindView(v, mContext, cursor);
return v;
}
@Override
public void bindView( View view , Context context , Cursor cursor )
{
ViewHolder holder = (ViewHolder) view.getTag();
holder.songNameTextView.setText(cursor.getString(2));
holder.artistTextView.setText(cursor.getString(1));
Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, cursor.getLong(3));
Picasso.with(getActivity()).load(albumArtUri).error(R.drawable.placeholder).placeholder(R.drawable.placeholder).into(holder.albumImageView);
}
@Override
public View newView( Context context , Cursor cursor , ViewGroup parent )
{
View view = LayoutInflater.from(context).inflate(R.layout.row_song, parent, false);
ViewHolder holder = new ViewHolder();
holder.albumImageView = (ImageView) view.findViewById(R.id.albumImg);
holder.songNameTextView = (TextView) view.findViewById(R.id.songNameTxtView);
holder.artistTextView = (TextView) view.findViewById(R.id.artistNameTxtView);
view.setTag(holder);
return view;
}
}
private static class ViewHolder
{
public ImageView albumImageView;
public TextView artistTextView;
public TextView songNameTextView;
}
@Override
public Loader<Cursor> onCreateLoader( int arg0 , Bundle arg1 )
{
String[] projection = { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ALBUM_ID };
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
return new CursorLoader(getActivity(), MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, selection, null, null);
}
@Override
public void onLoadFinished( Loader<Cursor> arg0 , Cursor data )
{
adapter.swapCursor(data);
}
@Override
public void onLoaderReset( Loader<Cursor> arg0 )
{
adapter.swapCursor(null);
}
}
关于java - 即使在 AsyncTask 中,ListView 性能也太慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21350282/
我用 cudaMemcpy()一次将 1GB 的数据精确复制到设备。这需要 5.9 秒。反之则需要 5.1 秒。这是正常的吗?函数本身在复制之前有这么多开销吗? 理论上,PCIe 总线的吞吐量至少应为
我正在尝试读取图像的大小并在其高度大于 150 时调整其边距。但是在运行这段代码时,我总是在控制台中得到一个“0”: var coverImg; coverImg =
我正在开发一个 iPhone 应用程序,其中包含一些标准的“相机”功能。保存到相机胶卷真的太慢了,在iPhone 4上大约需要四秒钟。有什么办法可以提高速度吗? 如果您查看默认的 iPhone 相
我创建了一个将图像转换为灰度的类。但它的工作速度太慢了。有没有办法让它运行得更快? 这是我的类(class): @implementation PixelProcessing SYNTHESIZE_S
我使用以下代码,结果是正确的,但 gethostbyaddr 需要大约 30 秒。 function IPAddrToName(IPAddr: string): string; var So
我有以下案例, public class Test { private static final int MAX_NUMBER = 10_00_00; public static vo
我已经正确添加了所有必需的 JARS: Ucanaccess 3.0.4 commons-lang-2.6 commons-logging-1.1.1 hsqldbd jackcess-2.1.3 我
我为特定功能构建了一个多处理密码破解程序(使用单词列表),与使用单个进程相比,它减少了一半的时间。 最初的问题是,它会向您显示破解的密码并终止工作人员,但剩余的工作人员将继续工作,直到他们用完可哈希的
我在我的一个 JSP 中引入了 Sencha 网格。本地 sencha 相当快,但在外部服务器上它太慢了。 我在这里按照部署说明进行操作 http://docs.sencha.com/ext-js/4
我的查询加载时间有很大问题。在这种情况下,我需要 hg_ft_won 列(表:值)中的值,用于 home_team_id 和 away_team_id(表:匹配)。 它确实可以正常工作。加载只需要很长
我现在正在学习不同类型的排序,我发现,从某个点开始,我的快速排序算法根本无法快速工作。 这是我的代码: class QuickSort { // partitioning arr
为什么要模式 [0123]123456|98765 比在 Java 中执行 [0123]123456 然后 98765 慢两倍?所以单独搜索它们比用 OR 执行更快。有人有解释吗? UPD 查看带有结
我有带 Assets 的 Android 应用程序。它们包含 20,000 多个文件,其中大部分是简单的文本或 png 文件,分为不同的文件夹和子文件夹。1 个单个文件的最大大小为 500kb,其中
您好,我在查询中添加了 GROUP_CONCAT 函数,该函数终止了我的查询:/。我的查询是: SELECT u.username,a.user_id,a.id,a.text,a.lang as fr
我正在寻找优化查询的想法。 目前,我有一个 4M 行的表,我只想检索引用的最后 1000 行: SELECT * FROM customers_material_events WHERE refere
我在我的应用程序中使用 NSURLConnection,我在其中扫描条形码,通过 NSURLConnection 发送 XML,Java 服务向我发回 XML。我的问题是,使用 Wifi 时,响应时间
当我运行以下程序时,执行大约需要 7 到 8 分钟。我真的不确定我哪里弄错了,因为这个程序执行起来要花很多时间。 public class Test { public stat
我正在使用 NSFetchResultsController 从数据库中接收项目(有 80.000 个项目)。 这是我的谓词:@"(desc CONTAINS[cd] %@)", [any text]
我在 x_data 中有一个 3x2000 numpy 数组,在 y_data 中有一个 1x2000 numpy 数组,我将其传递给此函数 regress 以给我一条回归线。它工作正常。问题是我正在
我正在做一个项目,我需要改变图像的亮度和对比度,它是亮度而不是亮度。所以我一开始的代码是 for (int y = 0; y (y, x); // read pixel (0,0)
我是一名优秀的程序员,十分优秀!