我编写了一个应用程序,它只是向我显示手机上所有可用的联系人。该应用程序运行良好。它可以检索联系人图像和显示名称。但是,在滚动列表时,应用程序有些滞后。我想知道如何消除这种滞后。同时,我想在没有可用的联系人图像时显示图像。我试图创建一个布尔标志,告诉我联系人图像光标是否为空,如果是,那么我将标志 (image_found) 设置为 false,然后尝试使用 setImageResource 设置图像,但它不起作用。好吧,然后我认为当没有图像时,blob 对象可能会得到一个空值,但它也很有效。这是我的 mail_activity 代码。
package legacy_systems.aggregatedcontactlist;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ListActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Cursor c = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
null,
null,
null,
ContactsContract.Contacts.DISPLAY_NAME+" ASC");
ArrayList<String> con_ids = new ArrayList<String>();
if(c!=null)
{
for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
con_ids.add(c.getString(c.getColumnIndex(ContactsContract.Contacts._ID)));
}
}
ListView ls = getListView();
MyAdapter ada = new MyAdapter(this, R.layout.listview, con_ids);
ls.setAdapter(ada);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
这是 MyAdapter 的代码
package legacy_systems.aggregatedcontactlist;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentUris;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyAdapter extends ArrayAdapter<String>{
Context cont;
boolean image_found = true;
ArrayList<String> ids;
public MyAdapter(Context context, int resource,
ArrayList<String> objects) {
super(context, resource, objects);
cont = context;
ids = objects;
// TODO Auto-generated constructor stub
}
public View getView(int position, View ConvertView, ViewGroup parent){
//For Contact Photo
Bitmap photo=null;
//Inflator Work
LayoutInflater infl = (LayoutInflater)cont.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = infl.inflate(R.layout.list_view, parent, false);
//Retreiving photo and Setting Photo
photo = BitmapFactory.decodeStream(retrievePhoto(ids.get(position)));
ImageView iv = (ImageView)row.findViewById(R.id.listicon);
iv.setImageBitmap(photo);
if(!image_found)
{
iv.setImageResource(R.drawable.person);
}
//Retrieving ContactName
TextView tv =(TextView)row.findViewById(R.id.listtext);
tv.setText(retrieveName(ids.get(position)));
return row;
}
private InputStream retrievePhoto(String Id)
{
Uri ContactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, Long.valueOf(Id));
Uri PhotoUri = Uri.withAppendedPath(ContactUri, Contacts.Photo.CONTENT_DIRECTORY);
Cursor c = cont.getContentResolver().query(PhotoUri, new String[]{Contacts.Photo.PHOTO}, null,null,null);
try
{
if(c.moveToFirst())
{
byte[] data = c.getBlob(0);
if(data!=null)
{
return new ByteArrayInputStream(data);
}
else
{
image_found = false;
}
}
}
finally
{
c.close();
}
return null;
}
private String retrieveName(String Id)
{
Uri ContactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, Long.valueOf(Id));
Cursor c = cont.getContentResolver()
.query(ContactUri,
new String[]{ContactsContract.Contacts.DISPLAY_NAME},
null,
null,
null);
if(c==null)
return null;
try
{
if(c.moveToFirst())
{
String name = c.getString(0);
return name;
}
}
finally
{
c.close();
}
return null;
}
}
我真的很高兴我能走到这一步。请帮助我继续我的冒险。
[编辑] 我已经实施了建议的事情,现在我无法检索单个联系人图像。我不知道问题是什么。我只在 MyAdapter.java 中进行了更改,并且我正在发布它的代码。
package legacy_systems.aggregatedcontactlist;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentUris;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyAdapter extends ArrayAdapter<String>{
Context cont;
boolean image_found = true;
ArrayList<String> ids;
public MyAdapter(Context context, int resource,
ArrayList<String> objects) {
super(context, resource, objects);
cont = context;
ids = objects;
// TODO Auto-generated constructor stub
}
public View getView(int position, View ConvertView, ViewGroup parent){
View row = ConvertView;
//For Contact Photo
Bitmap photo=null;
//Inflator Work
if(row==null)
{
LayoutInflater infl = (LayoutInflater)cont.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = infl.inflate(R.layout.list_view, parent, false);
}
//Retreiving photo and Setting Photo
ImageView iv = (ImageView)row.findViewById(R.id.listicon);
synchronized (iv) {
Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, Long.valueOf(ids.get(position)));
PhotoLoader loader = new PhotoLoader(iv, contactUri);
loader.execute();
}
//Retrieving ContactName
TextView tv =(TextView)row.findViewById(R.id.listtext);
tv.setText(retrieveName(ids.get(position)));
return row;
}
private String retrieveName(String Id)
{
Uri ContactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, Long.valueOf(Id));
Cursor c = cont.getContentResolver()
.query(ContactUri,
new String[]{ContactsContract.Contacts.DISPLAY_NAME},
null,
null,
null);
if(c==null)
return null;
try
{
if(c.moveToFirst())
{
String name = c.getString(0);
return name;
}
}
finally
{
c.close();
}
return null;
}
class PhotoLoader extends AsyncTask<Void, Void, Bitmap>
{
final WeakReference<ImageView> mView;
final Uri mUri;
public PhotoLoader(ImageView view, Uri uri) {
if(view == null)
{
throw new IllegalArgumentException("View Cannot be null");
}
if(uri == null)
{
throw new IllegalArgumentException("Uri cant be null");
}
mView = new WeakReference<ImageView>(view);
mUri = uri;
}
protected Bitmap doInBackground(Void...args){
Bitmap bitmap;
InputStream in = ContactsContract.Contacts.openContactPhotoInputStream(cont.getContentResolver(), mUri);
bitmap = BitmapFactory.decodeStream(in);
if(bitmap == null)
{ Resources mResources = cont.getResources();
bitmap = BitmapFactory.decodeResource(mResources, R.drawable.person);
}
return bitmap;
}
}
}
我还没有实现缓存。当这个问题解决后,我会尝试实现缓存和Holder。在此之前,请解决问题,
[重新编辑] 这次。图像都乱了。
package legacy_systems.aggregatedcontactlist;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentUris;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyAdapter extends ArrayAdapter<String>{
Context cont;
boolean image_found = true;
ArrayList<String> ids;
ImageView iv;
public MyAdapter(Context context, int resource,
ArrayList<String> objects) {
super(context, resource, objects);
cont = context;
ids = objects;
// TODO Auto-generated constructor stub
}
public View getView(int position, View ConvertView, ViewGroup parent){
View row = ConvertView;
//For Contact Photo
//Inflator Work
if(row==null)
{
LayoutInflater infl = (LayoutInflater)cont.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = infl.inflate(R.layout.list_view, parent, false);
}
//Retreiving photo and Setting Photo
iv = (ImageView)row.findViewById(R.id.listicon);
synchronized (this) {
Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, Long.valueOf(ids.get(position)));
PhotoLoader loader = new PhotoLoader(iv, contactUri);
loader.execute();
}
//Retrieving ContactName
TextView tv =(TextView)row.findViewById(R.id.listtext);
tv.setText(retrieveName(ids.get(position)));
return row;
}
private String retrieveName(String Id)
{
Uri ContactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, Long.valueOf(Id));
Cursor c = cont.getContentResolver()
.query(ContactUri,
new String[]{ContactsContract.Contacts.DISPLAY_NAME},
null,
null,
null);
if(c==null)
return null;
try
{
if(c.moveToFirst())
{
String name = c.getString(0);
return name;
}
}
finally
{
c.close();
}
return null;
}
class PhotoLoader extends AsyncTask<Void, Void, Bitmap>
{
final WeakReference<ImageView> mView;
final Uri mUri;
public PhotoLoader(ImageView view, Uri uri) {
if(view == null)
{
throw new IllegalArgumentException("View Cannot be null");
}
if(uri == null)
{
throw new IllegalArgumentException("Uri cant be null");
}
mView = new WeakReference<ImageView>(view);
mUri = uri;
}
protected Bitmap doInBackground(Void...args){
Bitmap bitmap;
InputStream in = ContactsContract.Contacts.openContactPhotoInputStream(cont.getContentResolver(), mUri);
bitmap = BitmapFactory.decodeStream(in);
return bitmap;
}
protected void onPostExecute(Bitmap bitmap)
{
if(bitmap == null)
{ Resources mResources = cont.getResources();
bitmap = BitmapFactory.decodeResource(mResources, R.drawable.person);
iv.setImageBitmap(bitmap);
return;
}
iv.setImageBitmap(bitmap);
}
}
}