0

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

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

RowItem 是一个 POJO。 Horizo​​ntalListView来自 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);
    }
}
4

1 回答 1

0

好吧,本教程实际上帮助解决了我的问题。

于 2013-03-11T06:56:55.670 回答