优化游标适配器



我正在尝试从数据库(大约 20k 行)获取数据并显示在列表视图中。我正在使用光标加载器 + 光标适配器:所有工作都很漂亮,但是当我快速滚动列表视图时,它仍然滞后。这是我的光标适配器:

public class IssueCursorAdapter extends CursorAdapter {
private LayoutInflater inflater;
private Context mContext;
public IssueCursorAdapter(Context context, Cursor c) {
    super(context, c);
    inflater = LayoutInflater.from(context);
    mContext = context;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    ViewHolder holder = new ViewHolder();
    View view = inflater.inflate(R.layout.item_issue, parent, false);
    holder.tvIssueStatus = (TextView) view.findViewById(R.id.tvIssueStatus);
    ...same code
    view.setTag(holder);
    return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.tvIssueStatus.setText(cursor.getString(cursor.getColumnIndex("StatusDesc")));
    .......same code
}
private static class ViewHolder {
    TextView tvIssueStatus;
    TextView tvIssueType;
            ...same code
}
}

如何优化它?

最好是使用惰性加载器,也请通过这个

这是我使用字母索引器的实现:

  private class CurAdapter extends CursorAdapter implements SectionIndexer{
    AlphabetIndexer mAlphabetIndexer;
    public CurAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
        mAlphabetIndexer = new AlphabetIndexer(c,
                c.getColumnIndex("Name"),"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
        mAlphabetIndexer.setCursor(c);
    }
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ((TextView) view.getTag(R.id.textView1)).setText(cursor.getString(cursor.getColumnIndexOrThrow("Name")));
        String manuplate = dateConvert(cursor.getString(cursor.getColumnIndexOrThrow("BirthDate")));
        if(manuplate.contains("0001")){
            manuplate = manuplate.replace("0001", "");
        }
        ((TextView) view.getTag(R.id.textView2)).setText((manuplate));
        ((TextView) view.getTag(R.id.textView1)).setTypeface(tf, Typeface.BOLD);
        ((TextView) view.getTag(R.id.textView2)).setTypeface(tf1); 
        String image = cursor.getString(cursor.getColumnIndexOrThrow("imageUri"));
        System.out.println("Image Uri is:"+image);
        if(image.contains("jpg") || image.contains("png")){
            
            
            /*  File IMG_FILE = new File(image);
            Options options = new BitmapFactory.Options();
            options.inScaled = false;
            options.inDither = false;
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            Bitmap bitmap = BitmapFactory.decodeFile(IMG_FILE.getAbsolutePath(), options);*/
            File f = new File(image);
            Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
            ((ImageView) view.getTag(R.id.imageView1)).setImageBitmap(bmp); 
            
            
        }else{
            Uri IMAGE_URI = Uri.parse(image);
            IMAGE_URI= Uri.withAppendedPath(IMAGE_URI, Contacts.Photo.CONTENT_DIRECTORY);
            try{
                ((ImageView) view.getTag(R.id.imageView1)).setImageURI(IMAGE_URI);
            }catch (Exception e){
                System.out.println(e);
            }
        }
        ///Buffered stream handling if no image is present display a default image. 
        if (((ImageView) view.getTag(R.id.imageView1)).getBackground()== null){
            ((ImageView) view.getTag(R.id.imageView1)).setBackgroundResource(R.drawable.default_img); 
        }
    }
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.contacts_list, null);
        TextView tv = (TextView) view.findViewById(R.id.textView1);
        TextView tv1 = (TextView) view.findViewById(R.id.textView2);
        ImageView iv = (ImageView) view.findViewById(R.id.imageView1);
        view.setTag(R.id.textView1, tv);
        view.setTag(R.id.textView2, tv1);
        view.setTag(R.id.imageView1, iv);
        return view;
    }
    @Override
    public int getPositionForSection(int sectionIndex) {
        return mAlphabetIndexer.getPositionForSection(sectionIndex);
    }
    @Override
    public int getSectionForPosition(int position) {
        return mAlphabetIndexer.getSectionForPosition(position);
    }
    @Override
    public Object[] getSections() {
        return mAlphabetIndexer.getSections();
    }
}

最新更新