picture_list

系统 1486 0

 

layout_picture_list.xml

 

 
picture_list
 

 

<? xml version = "1.0" encoding = "utf-8" ?>

< FrameLayout xmlns:android = "http://schemas.android.com/apk/res/android"

    android:layout_width = "fill_parent"

    android:layout_height = "fill_parent"

    android:background = "@drawable/app_bg"

    android:orientation = "vertical" >

 

    < ViewSwitcher

        android:id = "@+id/viewswitcher_news_top"

        android:layout_width = "match_parent"

        android:layout_height = "match_parent"

        android:layout_gravity = "center" >

    </ ViewSwitcher >

 

    < ListView

        android:id = "@+id/picture_id"

        android:layout_width = "match_parent"

        android:layout_height = "match_parent"

        android:cacheColorHint = "@color/transparent"

        android:divider = "#00000000"

         android:listSelector = "#00000000" />

 

</ FrameLayout >

 

 

 

 

 

PictureListActivity.java

 

 

package com.cps.media.ui.picture;

 

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.List;

 

import org.apache.http.HttpResponse;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.impl.client.DefaultHttpClient;

import org.json.JSONArray;

import org.json.JSONObject;

 

import com.cps.media.R;

import com.cps.media.model.PictureItem;

import com.cps.media.util.CONST;

import com.cps.media.util.Util;

 

import android.app.Activity;

import android.content.Intent;

import android.graphics.Bitmap;

import android.net.Uri;

import android.os.AsyncTask;

import android.os.Bundle;

import android.os.Handler;

import android.util.Log;

import android.view.View;

import android.view.ViewGroup;

import android.widget.AdapterView;

import android.widget.BaseAdapter;

import android.widget.ImageView;

import android.widget.ListView;

import android.widget.TextView;

import android.widget.ViewSwitcher;

import android.widget.AdapterView.OnItemClickListener;

 

public class PictureListActivity extends Activity {

 

    private ListView mListView;

 

    private MyAdapter mAdapter;

 

    private List<PictureItem> mList;

   

    private ViewSwitcher viewSwitcher;

   

    private int mId;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

       // TODO Auto-generated method stub

       super.onCreate(savedInstanceState);

       setContentView(R.layout.layout_picture_list);

 

       mListView = (ListView) findViewById(R.id.picture_id);

       mId = getIntent().getIntExtra("id", 1);

      

       viewSwitcher = (ViewSwitcher) findViewById(R.id.viewswitcher_news_top);

       viewSwitcher.addView(getLayoutInflater().inflate(

              R.layout.layout_progress_page, null));

      

       requestData();

    }

 

    private void requestData() {

       viewSwitcher.setVisibility(View.VISIBLE);

       viewSwitcher.showNext();

       Thread t = new Thread() {

           @Override

           public void run() {

              super.run();

              HttpClient client = new DefaultHttpClient();

              StringBuilder builder = new StringBuilder();

              int error = 1;

              JSONArray array = null;

              HttpGet myget = new HttpGet(CONST.URL_PICTURE_LIST.replace("{0}", mId+""));

 

              try {

                  HttpResponse response = client.execute(myget);

                  BufferedReader reader = new BufferedReader(

                         new InputStreamReader(response.getEntity()

                                .getContent()));

                  for (String s = reader.readLine(); s != null; s = reader

                         .readLine()) {

                     builder.append(s);

                  }

                  JSONObject jsonObject = new JSONObject(builder.toString());

                  error = jsonObject.getInt("errno");

                  String errormsg = jsonObject.getString("errmsg");

                  Log.v("wgp", "url=" + myget.getURI().toString());

                  Log.v("wgp", "error=" + error);

                  Log.v("wgp", "errormsg=" + errormsg);

                  if (error == 0) {

                     array = jsonObject.getJSONArray("data");

                     PictureItem item;

                     mList = new ArrayList<PictureItem>();

                     for (int i = 0; i < array.length(); i++) {

                         item = new PictureItem();

                         item.setId(array.getJSONObject(i).getInt("id"));

                         item.setTitle(array.getJSONObject(i).getString(

                                "title"));

                         item.setImgUrl(array.getJSONObject(i).getString(

                                "img"));

                         mList.add(item);

                     }

 

                     if (mList.size() == 0) {

                         handler.sendEmptyMessage(-1);

                     } else {

                         handler.sendEmptyMessage(1);

                     }

                  }

              } catch (Exception e) {

                  e.printStackTrace();

                  handler.sendEmptyMessage(-1);

              } finally {

 

              }

           }

       };

       t.start();

    }

 

    Handler handler = new Handler() {

 

       public void handleMessage(android.os.Message msg) {

           if (msg.what == 1) {

              mAdapter = new MyAdapter();

              mListView.setAdapter(mAdapter);

              mListView.setOnItemClickListener(mListItemListener);

           }

           viewSwitcher.setVisibility(View.INVISIBLE);

       }

    };

   

    private OnItemClickListener mListItemListener = new OnItemClickListener() {

 

       @Override

       public void onItemClick(AdapterView<?> parent, View view, int position,

              long id) {

           Intent intent = new Intent(Intent.ACTION_VIEW);

           intent.setDataAndType(Util.getHttpImageURI(mList.get(position).getImgUrl()), "image/*");

           startActivity(intent);

       }

 

    };

   

    class MyAdapter extends BaseAdapter {

      

       private AsyncImageTask mImageTask;

 

       @Override

       public int getCount() {

           // TODO Auto-generated method stub

           return mList.size();

       }

 

       @Override

       public Object getItem(int position) {

           // TODO Auto-generated method stub

           return mList.get(position);

       }

 

       @Override

       public long getItemId(int position) {

           // TODO Auto-generated method stub

           return mList.get(position).getId();

       }

 

       @Override

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

           ViewHolder holder;

           if (convertView == null) {

              holder = new ViewHolder();

              convertView = getLayoutInflater().inflate(

                      R.layout.layout_picture_list_item, null);

              holder.tv_name = (TextView) convertView

                     .findViewById(R.id.name_id);

              holder.tv_image = (ImageView) convertView

                     .findViewById(R.id.type_image_id);

              convertView.setTag(holder);

           } else {

               holder = (ViewHolder) convertView.getTag();

           }

          

           mImageTask = new AsyncImageTask(holder.tv_image);

           mImageTask.execute(mList.get(position).getImgUrl());

          

           holder.tv_name.setText(mList.get(position).getTitle());

 

           return convertView;

       }

 

        class ViewHolder {

           TextView tv_name;

           ImageView tv_image;

           int type;

       }

      

       private class AsyncImageTask extends AsyncTask<String, Integer, Object> {

 

           private ImageView image;

 

           public AsyncImageTask(ImageView image) {

              this.image = image;

           }

 

           @Override

           protected Object doInBackground(String... params) {

              try {

                  if(Util.CPSE_CACHE.exists())

                     return Util.getHttpImageURI(params[0]);

                  else{

                     return Util.getHttpBitmap(params[0]);

                  }

              } catch (Exception e) {

                  // TODO: handle exception

              }

              return null;

           }

 

           @Override

           protected void onPostExecute(Object result) {

              // TODO Auto-generated method stub

              super.onPostExecute(result);

              if (image != null && result != null) {

                  if(result instanceof Uri){

                     image.setImageURI((Uri)result);

                  }else if(result instanceof Bitmap){

                     image.setImageBitmap((Bitmap)result);

                  }

              } else if (image != null && result == null) {

                  image.setImageResource(R.drawable.news_default);

              }

           }

 

       }

    }

 

}

 

 

 

 

 

 

 

 

 

 

picture_list


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论