简体   繁体   English

使用expandablelistview在我的imageview中填充联系人图像

[英]populate contact images in my imageview using expandablelistview

ChatFirstTab.java 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 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;
  }
}

My question is, I want to display Contact images for the particular person. 我的问题是,我想显示特定人的联系人图像 Now the contact name is displaying correctly. 现在, 联系人姓名正确显示。 I want contact image also to be displayed along name. 我还希望联系人图像也随名字显示。

I have tried many method but nothing helped. 我尝试了很多方法,但没有任何帮助。 Is there any method? 有什么办法吗?

You can get both contact name and image by using the below code.. 您可以使用以下代码获取联系人姓名和图像。

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;
    }

To display the images in the list i am using UniversalImageLoader . 为了显示列表中的图像,我正在使用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);
                            }

My Adapter class: 我的适配器类:

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