【发布时间】:2012-01-12 04:22:21
【问题描述】:
我在主要活动上有一个ListView,它在左侧显示一个联系人图片,在每一行的右侧显示一个文本视图。一切正常,但问题是当应用程序启动时,当有很多消息时,用户必须等待大约 10 秒以上才能开始使用应用程序。
我试图做的是找到一种方法来逐步加载每一行,并避免等待每一行加载后才能开始使用应用程序。我试过AsyncTasks、Handlers、Threads、Hashmaps.. 但我可以找到一种方法来管理这个任务。
我的活动是一个 ListActivity。
活动的列表视图使用带有参数的自定义适配器(this,smsarray,picturearray)。
smsarray 是一个ArrayList<String>,包含每条短信的字符串,picturearray 是一个ArrayList<Bitmap>,其中包含每条短信的位图格式的联系人图片。
我有一个方法getSMSData(),它在光标的帮助下获取短信字符串和位图,并将字符串放在 smsarray 上的索引 0 处,并将位图放在图片数组上的索引 0 处,用于启动时的每个联系人。
public class MessageList extends ListActivity {
static List<String> smsarray; static List<Bitmap> picturearray;
static MessageListAdapter smsadapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
smsarray = new ArrayList<String>();
picturearray = new ArrayList<Bitmap>();
getSMSData();
smsadapter = new MessageListAdapter(this,smsarray,picturearray);
getListView().setAdapter(smsadapter);
}
public void getSMSData() {
Bitmap defaultbitmap = BitmapFactory.decodeResource(getResources(), R.drawable.default_contact_picture);
Bitmap contactbitmap;
Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox"),new String [] {"person","address","body","date"} , null, null,"date ASC");
String[] displayname = new String[]{ContactsContract.Contacts.DISPLAY_NAME};
String name;
String person;
String address;
String date;
String body;
int id; Cursor contactcursor;
while (cursor.moveToNext()) {
person = cursor.getString(cursor.getColumnIndex("person"));
address = cursor.getString(cursor.getColumnIndex("address"));
date = cursor.getString(cursor.getColumnIndex("date"));
body = cursor.getString(cursor.getColumnIndex("body"));
if (person != null){
contactcursor = getContentResolver().query(Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, person),displayname, null, null, null);
contactcursor.moveToFirst();
name = contactcursor.getString(0);
id = Integer.parseInt(person);
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(), ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id));
contactbitmap = null;
if (input == null) {
contactbitmap = defaultbitmap;
} else {
contactbitmap = BitmapFactory.decodeStream(input);
}
if (contactbitmap != null){
picturearray.add(0,contactbitmap);
}else {
picturearray.add(0,defaultbitmap);
}
smsarray.add(0,getResources().getString(R.string.From) +" "+name+" "+"\n"+getResources().getString(R.string.Number)+" "+address+" " +"\n"+getResources().getString(R.string.At) +" "+date+"\n"+getResources().getString(R.string.Message)+" "+body);
}
if (person == null) {
picturearray.add(0,defaultbitmap);
smsarray.add(0,getResources().getString(R.string.From) +" "+getResources().getString(R.string.Unknown)+" "+"\n"+getResources().getString(R.string.Number)+" "+address+" " +"\n"+getResources().getString(R.string.At) +" "+date+"\n"+getResources().getString(R.string.Message)+" "+body);
}
}
}
}
public class MessageListAdapter extends ArrayAdapter <String> {
final Context vcontext;
final List<String> varray;
final List<Bitmap> vpicture;
final LayoutInflater layoutinflater;
public MessageListAdapter (Context context,List<String> smsarray, List<Bitmap> picturearray) {
super(context, 0);
vcontext = context;
varray = smsarray;
vpicture = picturearray;
this.layoutinflater = LayoutInflater.from(context);
}
public int getCountpicture() {
return vpicture.size();
}
public Bitmap getItempicture(int position) {
return vpicture.get(position);
}
public long getItemIdpicture(int position) {
return position;
}
public int getCount() {
return varray.size();
}
public String getItem(int position) {
return varray.get(position);
}
public long getItemId(int position) {
return position;
}
public class ViewHolder {
TextView messagetext;
ImageView image;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null){
convertView = layoutinflater.inflate(R.layout.listitem, null);
holder = new ViewHolder ();
holder.messagetext = (TextView) convertView.findViewById(R.id.messagetext);
holder.image = (ImageView) convertView.findViewById(R.id.contactimage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.image.setImageBitmap(vpicture.get(position));
holder.image.setPadding(2, 1, 0, 0);
holder.messagetext.setText(varray.get(position));
holder.messagetext.setPadding(10, -1, 10, 5);
return convertView;
}
}
感谢您的宝贵帮助。
【问题讨论】:
标签: android listview android-widget