我想在ListView中显示所有用户收件箱短信。我使用一种方法首先获取所有短信(并保留在我的短信模型上),然后使用我的自定义BaseAdapter来显示。 但它很慢,有时在弱电设备中导致"没有响应"。 我猜是因为它获得了所有短信一次并且它不是懒惰的。有什么想法提高性能吗?
我的助手方法:
public List<SMS> getAllSMS() {
List<SMS> unreadSms = new ArrayList<SMS>();
Cursor cur = context.getContentResolver().query(SMS_INBOX, null, "", null, null);
while (cur.moveToNext()) {
SMS item = new SMS();
item.setId(cur.getInt(0));
item.setThreadId(cur.getInt(1));
item.setNumber(cur.getString(cur.getColumnIndexOrThrow("address")));
item.setName(getContactName(item.getNumber()));
item.setDate(new Date(cur.getLong(cur.getColumnIndexOrThrow("date"))));
item.setMessage(cur.getString(cur.getColumnIndexOrThrow("body")));
unreadSms.add(item);
}
cur.close();
return unreadSms;
}
我的适配器:
public class SMSAdapter extends BaseAdapter {
private Context context;
private List<SMS> items;
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
public SMSAdapter(Context context, List<SMS> items) {
this.context = context;
this.items = items;
}
@Override public int getCount() {
return items.size();
}
@Override public SMS getItem(int i) {
return items.get(i);
}
@Override public long getItemId(int i) {
return 0;
}
@Override public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
if (view == null) {
view = LayoutInflater.from(context).inflate(R.layout.item_sms_list, viewGroup, false);
viewHolder = new ViewHolder(view);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
final SMS item = getItem(i);
Uri contactUri = null;
try {
contactUri = PhoneUtil.getPhotoUri(context, item.getNumber());
} catch (Exception e) {
e.printStackTrace();
}
Picasso.with(context)
.load(contactUri)
.placeholder(R.mipmap.ic_avatar)
.into(viewHolder.imgAvatar);
if (item.getName() != null && !item.getName().isEmpty()) {
viewHolder.txtName.setText(item.getName());
} else {
viewHolder.txtName.setText(item.getNumber());
}
final PersianCalendar persianCalendar = new PersianCalendar();
persianCalendar.setTimeInMillis(item.getDate().getTime());
viewHolder.txtDate.setText(
simpleDateFormat.format(item.getDate()) + " " + persianCalendar.getPersianShortDate());
viewHolder.txtMessage.setText(item.getMessage());
viewHolder.imgDelete.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
//items.remove(item);
PhoneUtil.removeSMS(context, item.getId(), item.getThreadId());
notifyDataSetChanged();
}
});
return view;
}
private static class ViewHolder {
public ImageView imgAvatar, imgDelete;
public TextView txtName;
public TextView txtDate;
public TextView txtMessage;
public ViewHolder(View viewItem) {
final TypefaceProvider typefaceProvider = new TypefaceProvider(viewItem.getContext());
imgAvatar = (ImageView) viewItem.findViewById(R.id.img_avatar);
imgDelete = (ImageView) viewItem.findViewById(R.id.img_delete);
txtName = (TextView) viewItem.findViewById(R.id.txt_name);
txtDate = (TextView) viewItem.findViewById(R.id.txt_date);
txtMessage = (TextView) viewItem.findViewById(R.id.txt_message);
typefaceProvider.setTypeface(txtName);
typefaceProvider.setTypeface(txtDate);
typefaceProvider.setTypeface(txtMessage);
}
}
}
答案 0 :(得分:0)
我更改了我的适配器并使用CursorAdapter而不是BaseAdapter并且在加载我的适配器之前没有获得所有SMS。性能提高,滞后减少。
希望这对某人有所帮助。