2013-03-11 109 views
0

我一直在尋找這方面的一些答案在這裏,並嘗試了一些與我目前在我的代碼中的結合。我對某些部分有些疑惑。所以,這是我的代碼:Android從JSON加載圖像到Horizo​​ntalListView

MyMainActivity class。分析json並設置適配器。

RowItem是POJO。 HorizontalListView是從開發智能

. 
. 
. 
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); 
    } 
} 
+0

粘貼您的logcat跟蹤。 – dmnlk 2013-03-11 01:34:20

+0

我不能有一個logcat跟蹤,因爲我無法運行該應用程序。我想我的問題在於如何使用doinbackground獲取我的網址,然後將其傳遞給loadBitmap。然後設置我的位圖 – elL 2013-03-11 02:35:36

回答

0

那麼,這tutorial實際上幫助解決我的問題。

相關問題