加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

webService异步加载GridView中图片

发布时间:2020-12-17 00:58:26 所属栏目:安全 来源:网络整理
导读:效果如图: 类ViewCacheForRandom9: public class ViewCacheForRandom9 { private View baseView; private ImageView sex; private TextView distance; private ImageView imageView; public ViewCacheForRandom9(View baseView) { this.baseView = baseView

效果如图:

类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,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); ???? ?? ??} ???? ?????? ??? ???? ????????? }); ???????????? } ???????? });

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读