1

当我使用 listview 从 url 获取图像时,我在代码中使用了 ImageLoader 类来删除缓存并显示图像。看到我的代码fifthstep class,我使用CategoryListAdapter2加载的适配器类

imageLoader.DisplayImage(fifthstep.Category_image.get(position), activity, holder.imgThumb);

要在 imageview 中删除缓存并显示图像,但在下面的第三节课中,我没有直接使用 listview 来解析来自 url 的 json 数据。所以由于我有时会遇到错误,图像不会显示我如何在我的第三类中使用 Loader 类?看到这个例子 也使用了 Meory 缓存 ImageLoader 类我如何在我的第三个类中使用 sed tis 函数,它不使用列表视图

      public class fifthstep extends Activity {

      private HorizontalListView2 listview;


CategoryListAdapter2 cla;
      static ArrayList<Long> Category_ID = new ArrayList<Long>();
static ArrayList<String> Category_name = new ArrayList<String>();
static ArrayList<String> Category_image = new ArrayList<String>();
String CategoryAPI;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainmenulist);
               cla = new CategoryListAdapter2(fifthstep.this);








         CategoryAPI = Utils.CategoryAPI2;

 //     clearData();
    try {

        HttpClient client = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(client.getParams(), 
      15000);
        HttpConnectionParams.setSoTimeout(client.getParams(), 15000);
        HttpUriRequest request = new HttpGet(CategoryAPI);
        HttpResponse response = client.execute(request);
        InputStream atomInputStream = response.getEntity().getContent();
        BufferedReader in = new BufferedReader(new  
       InputStreamReader(atomInputStream));

        String line;
        String str = "";
        while ((line = in.readLine()) != null){
            str += line;
        }


            JSONObject json = new JSONObject(str);
            JSONArray data = json.getJSONArray("worldpopulation");


 //         //  JSONArray results =  json.getJSONArray("worldpopulation2");
        JSONObject person=json.getJSONObject("worldpopulation2"); 
            String Name=person.getString("describtion");
            String url2=person.getString("url");
            txtdescription.setText(Name);
             Bitmap bitmap = 
    BitmapFactory.decodeStream((InputStream)new URL(url2).getContent());
         imageView1.setImageBitmap(bitmap);

            for (int i = 0; i < data.length(); i++) {
                JSONObject object = data.getJSONObject(i); 

            //    JSONObject category = 
   object.getJSONObject("Category");


          Category_ID.add(Long.parseLong(object.getString("rank")));
                Category_name.add(object.getString("name"));
                Category_image.add(object.getString("url"));

                Log.d("Category name", Category_name.get(i));
                listview.setAdapter(cla);


            }




                     public class CategoryListAdapter2 extends BaseAdapter {

private Activity activity;
private ImageLoader imageLoader;

public CategoryListAdapter2(Activity act) {
    this.activity = act;
    imageLoader = new ImageLoader(act);
}

public int getCount() {
    // TODO Auto-generated method stub
    return fifthstep.Category_ID.size();
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

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

public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder holder;

    if(convertView == null){
        LayoutInflater inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.viewitem, null);
        holder = new ViewHolder();

        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }


    holder.txtText = (TextView) convertView.findViewById(R.id.title);
    holder.imgThumb = (ImageView) convertView.findViewById(R.id.image);

    holder.txtText.setText(fifthstep.Category_name.get(position));

        imageLoader.DisplayImage(fifthstep.Category_image.get(position),
            activity, holder.imgThumb);

    return convertView;
}

static class ViewHolder {
    TextView txtText;
    ImageView imgThumb;
}


//is working perfect every time display iamge from url//////////////



        public class thirdstep extends Activity {

        String MenuSelect;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.category_list2);

             ImageView img1 = (ImageView) findViewById(R.id.img1);

    TextView txt1 = (TextView) findViewById(R.id.txt1);

    ImageView img2 = (ImageView) findViewById(R.id.img2);

    TextView txt2 = (TextView) findViewById(R.id.txt2);

    ImageView img3 = (ImageView) findViewById(R.id.img3);

    TextView txt3 = (TextView) findViewById(R.id.txt3);


    MenuSelect = Utils.MenuSelect;

    try {

        HttpClient client = new DefaultHttpClient();
        HttpConnectionParams
                .setConnectionTimeout(client.getParams(), 15000);
        HttpConnectionParams.setSoTimeout(client.getParams(), 15000);
        HttpUriRequest request = new HttpGet(MenuSelect);
        HttpResponse response = client.execute(request);
        InputStream atomInputStream = response.getEntity().getContent();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                atomInputStream));

        String line;
        String str = "";
        while ((line = in.readLine()) != null) {
            str += line;
        }







                           JSONObject json = new JSONObject(str);
            JSONArray data = json.getJSONArray("worldpopulation");
            JSONObject jsonOrder = (JSONObject)data.get(0);

            txt1.setText (jsonOrder.getString("name"));

            String url1 = jsonOrder.getString("url");
            //

                            Bitmap bitmap = BitmapFactory
                                    .decodeStream((InputStream) new URL(url1).getContent());
                            img1.setImageBitmap(bitmap);


        //    img1.setImageBitmap( /* decoded url input stream*/);

            JSONObject jsonOrder2 = (JSONObject)data.get(1);
            txt2.setText (jsonOrder2.getString("name2"));

            String url2 = jsonOrder2.getString("url2");
            //

                            Bitmap bitmap2 = BitmapFactory
                                    .decodeStream((InputStream) new URL(url2).getContent());
                            img2.setImageBitmap(bitmap2);



         //   img2.setImageBitmap( /* decoded url input stream*/);

            JSONObject jsonOrder3 = (JSONObject)data.get(2);
            txt3.setText (jsonOrder3.getString("name3"));

            String url3 = jsonOrder3.getString("url3");
            //

                            Bitmap bitmap3 = BitmapFactory

    .decodeStream((InputStream) new URL(url3).getContent());
                            img3.setImageBitmap(bitmap3);
4

1 回答 1

0

我看到你所有关于 JSON + andorid 的问题 关注这些帖子,对于 AsyncTask 或 Json Parsing from a Url here

并且

Json Persing from a URL or a Image load here

关注博客/网站上的所有帖子,.. 这将对您有所帮助,..

只是理解这个概念,.. 这将帮助您高达 80%,..

于 2013-12-10T10:42:13.203 回答