webService异步加载GridView中图片

本文展示了如何通过自定义适配器和缓存机制,在GridView中动态加载并展示附近9个随机用户的性别、距离和头像信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果如图:

类ViewCacheForRandom9:

public class ViewCacheForRandom9 {  
    
    private View baseView;  
    private ImageView sex;  
    private TextView distance;  
    private ImageView imageView;  

    public ViewCacheForRandom9(View baseView) {  
        this.baseView = baseView;  
    }  

    public ImageView getSex() {  
        if (sex == null) {  
        	sex = (ImageView) baseView.findViewById(R.id.txt_gv_sex);  
        }  
        return sex;  
    }  
    public TextView getDistance() {  
        if (distance == null) {  
        	distance = (TextView) baseView.findViewById(R.id.txt_gv_distance);  
        }  
        return distance;  
    }  
    public ImageView getImageView() {  
        if (imageView == null) {  
            imageView = (ImageView) baseView.findViewById(R.id.img_gv_head);  
        }  
        return imageView;  
    }  

}  


类Random9GridViewAdapter:

public class Random9GridViewAdapter extends ArrayAdapter<NearPeople> {

private GridView gridView;
private AsyncImageLoader asyncImageLoader;
public Random9GridViewAdapter(Activity activity, List<NearPeople> near, GridView gridView1) {
super(activity, 0, near);
this.gridView = gridView1;
asyncImageLoader = new AsyncImageLoader();
}

public View getView(int position, View convertView, ViewGroup parent) {
Activity activity = (Activity) getContext();

// Inflate the views from XML
View rowView = convertView;
ViewCacheForRandom9 viewCache;
if (rowView == null) {
LayoutInflater inflater = activity.getLayoutInflater();
rowView = inflater.inflate(R.layout.griditem, null);
viewCache = new ViewCacheForRandom9(rowView);
rowView.setTag(viewCache);
} else {
viewCache = (ViewCacheForRandom9) rowView.getTag();
}
NearPeople near = getItem(position);

// Load the image and set it on the ImageView

String imageUrl = near.getheadUrl();
ImageView imageView = viewCache.getImageView();
imageView.setTag(imageUrl);
Drawable cachedImage = asyncImageLoader.loadDrawable(imageUrl, new ImageCallback() {
public void imageLoaded(Drawable imageDrawable, String imageUrl) {
ImageView imageViewByTag = (ImageView) gridView.findViewWithTag(imageUrl);
if (imageViewByTag != null) {
imageViewByTag.setImageDrawable(imageDrawable);
}
}
});
if (cachedImage == null) {
imageView.setImageResource(R.drawable.head1);
}else{
imageView.setImageDrawable(cachedImage);
}
ImageView sex = viewCache.getSex();
if(near.getsex().equals("男"))
{
sex.setBackgroundResource(R.drawable.male);
}
else
sex.setBackgroundResource(R.drawable.female);
// Set the text on the TextView
TextView textView = viewCache.getDistance();
textView.setText(String.valueOf(near.getdistance()));
return rowView;
}

}

真正调用:

//附近的人
txt_around.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
LinearLayout layout = (LinearLayout) inflater.inflate(
R.layout.peoplearound, null).findViewById(R.id.lin_aroundpeople);
GridView lv=(GridView)layout.getChildAt(0);
SoapObject so= WebServiceUtil.getRandom9PeopleList(spotid);
TextView morepeople = (TextView)layout.findViewById(R.id.txt_morepeople);
morepeople.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent1=new Intent(viewMessageActivity.this,PeopleAroundActivity.class);
viewMessageActivity.this.startActivity(intent1);
}

});
try
{
SoapObject detail = (SoapObject) so.getProperty("getRandom9PeopleList"
+ "Result");
Log.v("R",detail.toString());
List<NearPeople> list = new ArrayList<NearPeople>();
NearPeople[] n = new NearPeople[detail.getPropertyCount()];
for (int i=0;i<detail.getPropertyCount();i++)
{
n[i] = new NearPeople();
}

n=parsePeolist(detail);
for(int i=0;i<n.length;i++){
list.add(new NearPeople( n[i].getheadUrl(), n[i].getsex(),n[i].getdistance()));
}
lv.setAdapter(new Random9GridViewAdapter(viewMessageActivity.this,list,lv));
}
catch(Exception e){

Toast.makeText(viewMessageActivity.this,"该景点附近没有游客", Toast.LENGTH_LONG).show();
morepeople.setText("该景点附近没有游客");
morepeople.setClickable(false);

}
lin.removeAllViews();
lin.addView(layout);
lv.setOnItemClickListener(new OnItemClickListener(){

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Intent intent = new Intent(viewMessageActivity.this,PeopleInfoActivity.class);
intent.putExtra("peopleid","v10000001");
startActivity(intent);

}});


// TextView morepeople1 = (TextView)findViewById(R.id.txt_more);
morepeople.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent1=new Intent(viewMessageActivity.this,PeopleAroundActivity.class);
viewMessageActivity.this.startActivity(intent1);
}

});
}
});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值