我正在尝试使用图片在ListView
中显示收藏的联系人。我能够按字母顺序显示,但首先显示Favourite ICON
首选的优惠项目并不容易。这是我的代码
mProjection = new String[] {
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone._ID
};
final Cursor cursor = getActivity().getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
mProjection,
null,
null,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"
);
ContactListAdapter listAdapter = new ContactListAdapter(getActivity(), cursor, mProjection);
list = getListView();
list.setFastScrollEnabled(true);
list.setAdapter(listAdapter);
我的适配器类
public class ContactListAdapter extends BaseAdapter {
private Context mContext;
private Cursor mCursor;
private LayoutInflater inflater;
private String[] mProjection;
// State of the row that needs to show separator
private static final int SECTIONED_STATE = 1;
// State of the row that need not show separator
private static final int REGULAR_STATE = 2;
// Cache row states based on positions
private int[] mRowStates;
public ContactListAdapter(Context context, Cursor cursor, String[] projection){
mContext = context;
mCursor = cursor;
mRowStates = new int[getCount()];
mProjection = projection;
}
@Override
public int getCount() {
return mCursor.getCount();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
boolean showSeparator = false;
mCursor.moveToPosition(position);
if (inflater == null)
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
v = inflater.inflate(R.layout.contact_list_item, null);
TextView cntName = (TextView) v.findViewById(R.id.cntName);
TextView cntNumber = (TextView) v.findViewById(R.id.cntNumber);
String name = mCursor.getString( mCursor.getColumnIndex(mProjection[0]) );
String number = mCursor.getString( mCursor.getColumnIndex(mProjection[1]) );
int idCol = mCursor.getColumnIndex(mProjection[2]);
long id = mCursor.getLong(idCol);
cntName.setText(name);
cntNumber.setText(number);
// Show separator ?
switch (mRowStates[position]) {
case SECTIONED_STATE:
showSeparator = true;
break;
case REGULAR_STATE:
showSeparator = false;
break;
default:
if (position == 0) {
showSeparator = true;
}
else {
mCursor.moveToPosition(position - 1);
String previousName = mCursor.getString(mCursor.getColumnIndex(mProjection[0]));
char[] previousNameArray = previousName.toCharArray();
char[] nameArray = name.toCharArray();
if (nameArray[0] != previousNameArray[0]) {
showSeparator = true;
}
mCursor.moveToPosition(position);
}
// Cache it for later
mRowStates[position] = showSeparator ? SECTIONED_STATE : REGULAR_STATE;
break;
}
TextView separatorView = (TextView) v.findViewById(R.id.acronym);
TextView separatorView1 = (TextView) v.findViewById(R.id.acronym1);
ImageView croppedProfile = (ImageView) v.findViewById(R.id.croppedProfile);
InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(mContext.getContentResolver(),
ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id));
Bitmap bitmap= BitmapFactory.decodeStream(inputStream);
if (bitmap == null){
croppedProfile.setImageResource(R.drawable.round_draw);
} else {
croppedProfile.setImageBitmap(bitmap);
}
if (showSeparator) {
separatorView.setText(name.toCharArray(), 0, 1);
//separatorView.setBackgroundColor(mContext.getResources().getColor(R.color.transparent));
separatorView.setVisibility(View.VISIBLE);
separatorView1.setVisibility(View.GONE);
}
else {
//separatorView.setBackgroundColor(mContext.getResources().getColor(R.color.transparent));
//croppedProfile
separatorView.setVisibility(View.GONE);
separatorView1.setVisibility(View.VISIBLE);
/*LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(80, 0, 0, 0);
margLayout.setLayoutParams(layoutParams);*/
}
return v;
}
}