测试通过机型
HTC U11(8.0)
OPPO Finder (4.0.3)
HUAWEI C8815 (4.1.2)
package com.example.listviewtest;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<Fruit> fruitList = new ArrayList<Fruit>();
FruitAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFruits(); // 初始化水果数据
adapter = new FruitAdapter(MainActivity.this, R.layout.fruit_item, fruitList);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Fruit fruit = fruitList.get(position);
Toast.makeText(MainActivity.this, fruit.getName(), Toast.LENGTH_SHORT).show();
}
});
}
private synchronized static Bitmap getPhoto(Context c, Long raw_contact_id) {
Uri dataUri = Uri.parse("content://com.android.contacts/data");
// 根据contact_id从data表中查询出相应的电话号码和联系人名称, 实际上查询的是视图view_data
Cursor dataCursor = c.getContentResolver().query(dataUri,
//获取 当在mimetype_id的数据为vnd.android.cursor.item/photo与
// raw_contact_id数据为raw_contact_id的列
// 里面的data15有数据的数据
new String[]{"data15"},
// "mimetype='vnd.android.cursor.item/photo' AND raw_contact_id like '%"+raw_contact_id +"%'",
"mimetype='vnd.android.cursor.item/photo' AND raw_contact_id=" + raw_contact_id,
null, null);
Bitmap bitmap = null;
while (dataCursor.moveToNext()) {
byte[] str = dataCursor.getBlob(0);
if (str != null) {
InputStream inputStream = new ByteArrayInputStream(str);
bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
continue;
}
return bitmap;
}
private void initFruits() {
Cursor cursor = null;
try {
// 查询联系人数据
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
// 获取联系人姓名
String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
// 获取联系人手机号
String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Long contactId = cursor.getLong(cursor.getColumnIndex(ContactsContract.Contacts.NAME_RAW_CONTACT_ID));
Fruit apple = new Fruit(displayName, number, getPhoto(this, contactId));
fruitList.add(apple);
}
adapter.notifyDataSetChanged();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}
package com.example.listviewtest;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class FruitAdapter extends ArrayAdapter<Fruit> {
private int resourceId;
public FruitAdapter(Context context, int textViewResourceId,
List<Fruit> objects) {
super(context, textViewResourceId, objects);
resourceId = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Fruit fruit = getItem(position); // 获取当前项的Fruit实例
View view;
ViewHolder viewHolder;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
viewHolder = new ViewHolder();
viewHolder.fruitImage = (ImageView) view.findViewById (R.id.fruit_image);
viewHolder.fruitName = (TextView) view.findViewById (R.id.fruit_name);
viewHolder.fruitNum = (TextView) view.findViewById (R.id.fruit_num);
view.setTag(viewHolder); // 将ViewHolder存储在View中
} else {
view = convertView;
viewHolder = (ViewHolder) view.getTag(); // 重新获取ViewHolder
}
viewHolder.fruitNum.setText(fruit.getNum());
viewHolder.fruitImage.setImageBitmap(fruit.getImageBitmap());
viewHolder.fruitName.setText(fruit.getName());
return view;
}
class ViewHolder {
ImageView fruitImage;
TextView fruitName;
TextView fruitNum;
}
}
package com.example.listviewtest;
import android.graphics.Bitmap;
public class Fruit {
private String name;
private String num;
private int imageId;
private Bitmap imageBitmap;
// public Fruit(String name, String num, int imageId) {
// this.num = num;
// this.name = name;
// this.imageId = imageId;
// }
public Fruit(String name, String num, Bitmap imageBitmap) {
this.num = num;
this.name = name;
this.imageBitmap = imageBitmap;
}
public String getName() {
return name;
}
public Bitmap getImageBitmap() {
return imageBitmap;
}
public int getImageId() {
return imageId;
}
public String getNum() {
return num;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="100dp">
<ImageView
android:id="@+id/fruit_image"
android:layout_width="70dp"
android:layout_height="70dp" />
<TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp" />
<TextView
android:id="@+id/fruit_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
<uses-permission android:name="android.permission.READ_CONTACTS" />
上面代码没有加权限申请,加上就好
网友评论