gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:22:53 24 4
gpt4 key购买 nike

我编写了一个简单的应用程序来显示联系人和图像。我收到联系人姓名和电话号码,但图像未进入 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

最佳答案

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

关于android - 如何在android中的 ListView 中显示联系人详细信息和图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21901983/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com