Android将图像从JSON加载到Horizo​​ntalListView

时间:2013-03-11 01:16:06

标签: android json android-asynctask

我在这里一直在寻找关于此问题的一些答案,并尝试将其中的一些与我目前在代码中的内容相结合。我对某些部分有些困惑。所以,这是我的代码:

MyMainActivity 类。解析json并设置适配器。

RowItem是一个POJO。 HorizontalListView来自dev-smart

.
.   
.
HorizontalListView hListView;
List<RowItem> sItems;

public void onCreate(Bundle savedInstanceState){
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Intent intent = getIntent();
    String categoryId = intent.getStringExtra("id");

    JSONParser parser = new JSONParser(this); 
    JSONObject json = parser.getJSONFromAssets("mylist.json");

    shopItems = new ArrayList<ShopRowItem>();

    try{
        shops = json.getJSONArray(LIST_ARR);
        for(int i = 0; i < shops.length(); i++){
            JSONObject c = jsonArray.getJSONObject(i);

            String sName = c.getString(NAME);
            String sCatId = c.getString(ID);
            String imageUrl = c.getString(IMAGE_URL);

            RowItem item = new ShopRowItem(imageUrl, sName, sCatId);
            sItems.add(item);

        }
    }catch(JSONException e){
        throw new RuntimeException(e);
    }

    hListView = (HorizontalListView) findViewById(R.id.hlist_view);
    ShopBaseAdapter adapter = new ShopBaseAdapter(this, sItems);
    hListView.setAdapter(adapter); 

然后,我的 CustomAdapter 类扩展到BaseAdapter。

 .
 .
 .
    Context context;
List<RowItem> rowItems;



private class ViewHolder{
    ImageView imageView;
    TextView txtName;
    TextView txtId;
}

public View getView(int position, View convertView, ViewGroup parent){
    ViewHolder holder = null;

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if(convertView == null){
        convertView = inflater.inflate(R.layout.shop_list_layout, null);
        holder = new ViewHolder();
        holder.txtShopName = (TextView) convertView.findViewById(R.id.name);
        holder.txtCatId = (TextView) convertView.findViewById(R.id.cat_id);


        holder.imageView = (ImageView) convertView.findViewById(R.id.prof_pic);
        holder.imageView.getLayoutParams().height = 120;
        holder.imageView.getLayoutParams().width = 120;
        holder.imageView.setPadding(5, 5, 5, 5);
        holder.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

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

    RowItem sItem = (RowItem) getItem(position);

    holder.txtShopName.setText(sItem.getName());
    holder.txtCatId.setText(sItem.getCatId());


    try{
                        **// I dont know what to do here  
        holder.imageView.setImageBitmap( ? ? ? ?)** 
    }catch(Exception e){

    }

    return convertView;
}

然后我有 MyImageLoader 类扩展了AsyncTask。

public class ShopImageLoader extends AsyncTask<Object, String, Bitmap>{
.
.
.


**protected Bitmap doInBackground(Object... params){
    //how to get the url from other class? ?
    //then loadBitmap(url);
            // then set it for dispaly
    //i'm really confuse on what to do here.
    return bitmap; ??

}**



public static Bitmap loadBitmap(String url){
    Bitmap bitmap = null;
    InputStream in = null;
    BufferedOutputStream out = null;

    try{
        in  = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);
        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
        out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
        copy(in, out);
        out.flush();

        final byte[] data = dataStream.toByteArray();
        BitmapFactory.Options options = new BitmapFactory.Options();

        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);
    }catch(IOException e){
        Log.e(TAG, "Could not load Bitmap from: "+ url);
    }finally{
        closeStream(in);
        closeStream(out);
    }

    return bitmap;
}

private static void closeStream(Closeable stream){
    if(stream != null){
        try{
            stream.close();
        }catch(IOException e){
            android.util.Log.e(TAG, "Could not close stream", e);
        }
    }
}

private static void copy(InputStream in, BufferedOutputStream out) throws IOException {
    byte[] byt = new byte[IO_BUFFER_SIZE];
    int read;
    while((read = in.read(byt)) != -1){
        out.write(byt, 0, read);
    }
}

1 个答案:

答案 0 :(得分:0)

嗯,这tutorial实际上有助于解决我的问题。