簡體   English   中英

使用expandablelistview在我的imageview中填充聯系人圖像

[英]populate contact images in my imageview using expandablelistview

ChatFirstTab.java

public class ChatFirstTab extends Fragment{

ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
String name, photo;

@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.chat_first, container, false);

    expListView = (ExpandableListView) rootView.findViewById(R.id.expandableListView1);

    prepareListData();

    listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);

    expListView.setAdapter(listAdapter);

    return rootView;
}

private void prepareListData() {
    listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<String>>();

    Cursor cursor = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
            null, null, null, null);

    int count = cursor.getCount();

    while(cursor.moveToNext()) {
        name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        Log.d("name : ", name);
        listDataHeader.add(name);
    }

    cursor.close();

    // Adding child data
    List<String> request = new ArrayList<String>();
    request.add("Send Request");

    int i;

    for(i=0; i<count; i++)
    {
        listDataChild.put(listDataHeader.get(i), request); // Header, Child data
    }
  }
}

ExpandableListAdapter.java

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;

public ExpandableListAdapter(Context context, List<String> listDataHeader,
        HashMap<String, List<String>> listChildData) {
    this._context = context;
    this._listDataHeader = listDataHeader;
    this._listDataChild = listChildData;
}

@Override
public Object getChild(int groupPosition, int childPosititon) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .get(childPosititon);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public View getChildView(int groupPosition, final int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {

    final String childText = (String) getChild(groupPosition, childPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.chat_first_list_items, null);
    }

    TextView txtListChild = (TextView) convertView.findViewById(R.id.textView1);

    txtListChild.setText(childText);
    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .size();
}

@Override
public Object getGroup(int groupPosition) {
    return this._listDataHeader.get(groupPosition);
}

@Override
public int getGroupCount() {
    return this._listDataHeader.size();
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    String headerTitle = (String) getGroup(groupPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.chat_first_list_group, null);
    }

    TextView lblListHeader = (TextView) convertView.findViewById(R.id.textView1);
    lblListHeader.setText(headerTitle);

    return convertView;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
  }
}

我的問題是,我想顯示特定人的聯系人圖像 現在, 聯系人姓名正確顯示。 我還希望聯系人圖像也隨名字顯示。

我嘗試了很多方法,但沒有任何幫助。 有什么辦法嗎?

您可以使用以下代碼獲取聯系人姓名和圖像。

private String getContact(String ph)
    {
        String contactName = null;
        try{
            Uri uri = Uri.withAppendedPath(
                    PhoneLookup.CONTENT_FILTER_URI,
                    Uri.encode(ph));
            ContentResolver cr=getActivity().getContentResolver();
            Cursor cursor1 = cr.query(uri, new String[]{PhoneLookup.DISPLAY_NAME,PhoneLookup.PHOTO_URI}, null, null, null);
            if (cursor1 == null) {
                return "";
            }

            if(cursor1.moveToFirst()) {
                contactName = cursor1.getString(cursor1.getColumnIndex(PhoneLookup.DISPLAY_NAME));
                image_uri=cursor1.getString(cursor1.getColumnIndex(PhoneLookup.PHOTO_URI));
                cursor1.close();
            }
        }
        catch (Exception e) {
            // TODO: handle exception
        }
        return contactName;
    }

為了顯示列表中的圖像,我正在使用UniversalImageLoader

ImageLoader imageLoader = null;
                            imageLoader =ImageLoader.getInstance();
                            if(image_uri!=null)
                            {

                                options = new DisplayImageOptions.Builder()
                                        .showImageOnFail(R.drawable.profile)
                                        .showImageForEmptyUri(R.drawable.profile)
                                        .showImageOnLoading(R.drawable.profile)
                                        .cacheInMemory(true)
                                        .cacheOnDisc(true)
                                        .considerExifParams(true)
                                        .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
                                        .displayer(new RoundedBitmapDisplayer(200)).build();


                                imageLoader.displayImage(image_uri, img,options);
                            }
                            else
                            {
                                imageLoader.displayImage("drawable://" +R.drawable.profile, img,options);
                            }

我的適配器類:

class CustomAdapter extends ArrayAdapter<String>
    {

        Context context;
        ImageLoader imageLoader = null;

        public CustomAdapter(Context context)
        {
            super(context,0);
            this.context=context;
            sectionIndices=getSectionIndices();
            sectionDates=getSectionDates();
            imageLoader =ImageLoader.getInstance();
            options = new DisplayImageOptions.Builder()
                    .considerExifParams(true)
                    .showImageOnFail(R.drawable.profile)
                    .cacheInMemory(true)
                    .cacheOnDisc(true)
                    .considerExifParams(true)
                    .showImageForEmptyUri(R.drawable.profile)
                    .showImageOnLoading(R.drawable.profile)
                    .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
                    .displayer(new RoundedBitmapDisplayer(200)).build();
        }

        @Override
        public int getCount() {
            int size=0;
            try
            {
                size=db1.size();

            }
            catch(NullPointerException e)
            {

            }
            return size;
        }



        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater= LayoutInflater.from(context);
            if(convertView==null)
            {
                convertView=inflater.inflate(R.layout.main_log_screen, null);
            }
            final TextView tv= (TextView)convertView.findViewById(R.id.textView1);
            TextView tv2=(TextView)convertView.findViewById(R.id.textView2);
            ImageView pic=(ImageView)convertView.findViewById(R.id.imageView1);



            RelativeLayout rel=(RelativeLayout)convertView.findViewById(R.id.relativeLayout1);


            String name=getContact(db1.get(position).phone);
            if(name!=null)
            {
                tv.setText(name);
            }
            else
            {
                tv.setText(db1.get(position).phone);
            }


            if(photoUri.get(position)!=null)
            {
                imageLoader.displayImage(photoUri.get(position), pic,options);
            }
            else
            {
                imageLoader.displayImage("drawable://" +R.drawable.profile, pic, options);
            }



            return convertView;
        }





    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM