ここでこの問題に関するいくつかの回答を調べていて、現在コードにあるものと組み合わせていくつか試してみました。いくつかの部分でいくつかの混乱があります。だから、これは私のコードです:
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);
}
}