如何从服务器获取图像

时间:2013-07-23 07:57:42

标签: android image listview

class LoadLatest extends AsyncTask<String,Void,String>{

    ProgressDialog mProgress;

@Override
protected void onPreExecute() {

    mProgress=new ProgressDialog(MainActivity.mContext);
    mProgress.setTitle("Fetech Latest Videos");
    mProgress.setMessage("Please Wait...");
    mProgress.setCancelable(false);
    mProgress.show();
}   


    @Override
    protected String doInBackground(String... params) {

        try{
            JSONParser jParser = new JSONParser();
            String url="http://twominenglish.com/api/getlatest?";
            //http://twominenglish.com/api/getlatest?page=1
            JSONArray IdArray = jParser.getJSONFromUrl(url+"page="+page);
            mLatestList.clear();
            for(int i=0;i<IdArray.length();i++){

                try{
                     JSONObject jObject;
                     mLtest=new Latest();
                     jObject=IdArray.getJSONObject(i);
                     mLtest.SetID(jObject.getString("ID"));
                     //mLtest.SetImageUrl(jObject.getString("ImageURL"));
                     String path="http://twominenglish.com"+jObject.getString("ImageURL");
                     mLtest.SetImageUrl(path);
                     mLtest.SetDescription(jObject.getString("Description"));
                     mLtest.SetTitle(jObject.getString("Title"));
                     mLatestList.add(mLtest);
                     mLtest=new Latest();
                }catch(JSONException e){
                    e.printStackTrace();
                }
            }


        }catch(Exception e){
            e.printStackTrace();

        }


        // TODO Auto-generated method stub
        return null;
    }

// mY ADAPTER CLASS

public class LatestAdapter extends BaseAdapter{


private static ArrayList<Latest> ArrayList;
private Activity activity;
private LayoutInflater mInflater=null;
private Context mContext;
AvatarDownloader ad;
public ImageLoader mImageLoader;
public LatestAdapter(Activity context, ArrayList<Latest> results) {
    activity =context;
    ArrayList = results;
    mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    //mContext=context;
    mImageLoader=new ImageLoader(context.getApplicationContext());
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return ArrayList.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return ArrayList.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    final ViewHolder holder;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.latest_category, null);
        holder = new ViewHolder();

        holder.tv_Title = (TextView) convertView.findViewById(R.id.txtsubcatTitle);         
        holder.imgvw_pic = (ImageView) convertView.findViewById(R.id.imageView_pic);
         //ad=new AvatarDownloader(mContext);
        mImageLoader=new ImageLoader(activity);
        mImageLoader.DisplayImage(ArrayList.get(position).GetImageUrl(), ArrayList.get(position).GetTitle(), null, holder.imgvw_pic);
        String url=ArrayList.get(position).GetImageUrl().toString();
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.tv_Title.setText(ArrayList.get(position).GetTitle());



    return convertView;
}

static class ViewHolder {
    TextView tv_Title;
    ImageView imgvw_pic;
}

任何人都可以帮助这是我的适配器类它从ArrayList获取20个url当我通过图像加载器显示图像时它只运行7次。我做了什么。请帮助我..

1 个答案:

答案 0 :(得分:1)

您可以使用以下其中一个

1)懒惰列表 - https://github.com/thest1/LazyList

2)通用图像加载器 - https://github.com/nostra13/Android-Universal-Image-Loader

3)Volley-如果您看到谷歌io-2013视频。凌空应该非常快。