我正在尝试从数据库中获取数据(大约20k行)并在listview中显示。 我正在使用cursorloader + cursoradapter: 一切都很漂亮,但是当我快速滚动listview时它仍然落后。 这是我的游标适配器:
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
}
}
我如何优化它?
答案 0 :(得分:1)
以下是我使用字母索引器的实现:
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();
}
}