如何在android中的listview中显示联系人详细信息和图像

时间:2014-02-20 08:20:48

标签: android android-listview android-arrayadapter

我写了一个简单的应用程序,显示联系人和图像。我正在获取联系人姓名和电话号码,但图像未进入imageview。所以任何人都请帮助我。 提前致谢。这是我的代码:

 public class MainActivity extends Activity {

    // Add a list to keep all the "name: number" strings
    private List<String> mNameNumber = new ArrayList<String>();
    private static final String TAG = MainActivity.class.getName();

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Cursor c = getContentResolver().query(
        ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        String contactName, contactTelNumber = "";
        String contactID;
        String image_uri = "";
        Bitmap bitmap = null;
        // You only need to find these indices once
        int idIndex = c.getColumnIndex(ContactsContract.Contacts._ID);
        int hasNumberIndex = c
                .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
        int nameIndex = c
                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);


        // This is simpler than calling getCount() every iteration
        while (c.moveToNext()) {
            contactName = c.getString(nameIndex);
            contactID = c.getString(idIndex);
            image_uri = c.getString(photoIndex);
            Log.d(TAG, "Image" + image_uri + "contactName" + contactName);

            ImageView profile = (ImageView) findViewById(R.id.imageView1);

              try {
            Bitmap mybtmp = BitmapFactory.decodeStream((InputStream)new URL(image_uri).getContent());
                profile.setImageBitmap(mybtmp);
            Log.d(TAG, "bitmap"+mybtmp);
              } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            //setImageBitmap(my_btmp);

            // If this is an integer ask for an integer
            if (c.getInt(hasNumberIndex) > 0) {
                Cursor pCur = getContentResolver().query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = ?", new String[] { contactID }, null);
                while (pCur.moveToNext()) {
                    contactTelNumber = pCur
                            .getString(pCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                    // Store the "name: number" string in our list
                    mNameNumber.add(contactName + ": " + contactTelNumber);
                }
            }
        }

        // Find the ListView, create the adapter, and bind them
        ListView listView = (ListView) findViewById(R.id.listView1);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, mNameNumber);
        listView.setAdapter(adapter);
    }

}

我收到以下错误:

02-20 15:32:43.373: E/Trace(9762): error opening trace file: No such file or directory (2)
    02-20 15:32:43.385: V/ActivityThread(9762): Class path: /data/app/com.example.buddychat-1.apk, JNI path: /data/data/com.example.buddychat/lib
    02-20 15:32:43.567: D/com.example.buddychat.MainActivity(9762): ImagenullcontactNameDasarath Laptop
    02-20 15:32:43.567: W/System.err(9762): java.net.MalformedURLException
02-20 15:32:43.572: W/System.err(9762):     at java.net.URL.<init>(URL.java:154)
02-20 15:32:43.572: W/System.err(9762):     at java.net.URL.<init>(URL.java:127)
02-20 15:32:43.572: W/System.err(9762):     at com.example.buddychat.MainActivity.onCreate(MainActivity.java:77)
02-20 15:32:43.573: W/System.err(9762):     at android.app.Activity.performCreate(Activity.java:5247)
02-20 15:32:43.573: W/System.err(9762):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
02-20 15:32:43.573: W/System.err(9762):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
02-20 15:32:43.573: W/System.err(9762):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2225)
02-20 15:32:43.574: W/System.err(9762):     at android.app.ActivityThread.access$600(ActivityThread.java:151)
02-20 15:32:43.574: W/System.err(9762):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1301)
02-20 15:32:43.574: W/System.err(9762):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-20 15:32:43.574: W/System.err(9762):     at android.os.Looper.loop(Looper.java:153)
02-20 15:32:43.575: W/System.err(9762):     at android.app.ActivityThread.main(ActivityThread.java:5071)
02-20 15:32:43.575: W/System.err(9762):     at java.lang.reflect.Method.invokeNative(Native Method)
02-20 15:32:43.575: W/System.err(9762):     at java.lang.reflect.Method.invoke(Method.java:511)
02-20 15:32:43.575: W/System.err(9762):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
02-20 15:32:43.576: W/System.err(9762):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
02-20 15:32:43.576: W/System.err(9762):     at dalvik.system.NativeStart.main(Native Method)

02-20 15:32:44.344: W/System.err(9762): java.net.MalformedURLException
02-20 15:32:44.345: W/System.err(9762):     at java.net.URL.<init>(URL.java:154)
02-20 15:32:44.345: W/System.err(9762):     at java.net.URL.<init>(URL.java:127)
02-20 15:32:44.345: W/System.err(9762):     at com.example.buddychat.MainActivity.onCreate(MainActivity.java:77)
02-20 15:32:44.346: W/System.err(9762):     at android.app.Activity.performCreate(Activity.java:5247)
02-20 15:32:44.346: W/System.err(9762):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
02-20 15:32:44.346: W/System.err(9762):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
02-20 15:32:44.346: W/System.err(9762):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2225)
02-20 15:32:44.347: W/System.err(9762):     at android.app.ActivityThread.access$600(ActivityThread.java:151)
02-20 15:32:44.347: W/System.err(9762):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1301)
02-20 15:32:44.347: W/System.err(9762):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-20 15:32:44.347: W/System.err(9762):     at android.os.Looper.loop(Looper.java:153)
02-20 15:32:44.347: W/System.err(9762):     at android.app.ActivityThread.main(ActivityThread.java:5071)
02-20 15:32:44.348: W/System.err(9762):     at java.lang.reflect.Method.invokeNative(Native Method)
02-20 15:32:44.348: W/System.err(9762):     at java.lang.reflect.Method.invoke(Method.java:511)
02-20 15:32:44.348: W/System.err(9762):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
02-20 15:32:44.348: W/System.err(9762):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
02-20 15:32:44.349: W/System.err(9762):     at dalvik.system.NativeStart.main(Native Method)
02-20 15:32:44.372: D/com.example.buddychat.MainActivity(9762): Imagecontent://com.android.contacts/contacts/21/photocontactNameShaik Javeed

2 个答案:

答案 0 :(得分:2)

实现自定义列表视图的更好方法是使用自定义适配器。以下网址为初学者提供了很好的示例 URL

答案 1 :(得分:0)

这是我的代码,用于显示与我的设备上的图像联系。 第1步:获取照片:

String urlThumbnail = phones
                .getString(phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));

您可以使用“人物”项目上的格式字符串保存此网址。 第2步:在ImageView上显示图像。

if (!itemContact.getUrlThumbnail().equals("")) {
            final String thumbnailUrl = itemContact.getUrlThumbnail();
            if (thumbnailUrl != null) {
                final Bitmap thumbnail = fetchThumbnail(thumbnailUrl);
                if (thumbnail != null) {
                    holder.avataContact.setImageBitmap(thumbnail);
                }
            }
        } else {
            holder.avataContact.setImageDrawable(getContext().getResources().getDrawable(R.drawable.ic_contact_picture));
        }

// --------这是代码显示图片。

private Bitmap fetchThumbnail(String image_uri) {
    Bitmap bitmap = null;
    if (image_uri != null) {
        System.out.println(Uri.parse(image_uri));
        try {
            bitmap = MediaStore.Images.Media.getBitmap(mContext.getContentResolver(), Uri.parse(image_uri));
            System.out.println(bitmap);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return bitmap;
}

GL Bro ..