gpt4 book ai didi

java - 使用可扩展 ListView 在我的 ImageView 中填充联系人图像

转载 作者:行者123 更新时间:2023-11-30 02:03:57 24 4
gpt4 key购买 nike

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





}

关于java - 使用可扩展 ListView 在我的 ImageView 中填充联系人图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31042122/

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