Android将图像从JSON加载到HorizontalListView
在这里,我一直在寻找一些关于这个问题的答案,并尝试将其中一些与我目前代码中的内容结合起来。我对某些部分有些困惑。这是我的代码: MyMainActivity课程。解析json并设置适配器 RowItem是一个POJO。 是dev smart公司的Android将图像从JSON加载到HorizontalListView,android,json,android-asynctask,Android,Json,Android Asynctask,在这里,我一直在寻找一些关于这个问题的答案,并尝试将其中一些与我目前代码中的内容结合起来。我对某些部分有些困惑。这是我的代码: MyMainActivity课程。解析json并设置适配器 RowItem是一个POJO。 是dev smart公司的 . . . HorizontalListView hListView; List<RowItem> sItems; public void onCreate(Bundle savedInstanceState){ reque
.
.
.
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);
。
.
.
水平视图;
列出网站;
创建时的公共void(Bundle savedInstanceState){
requestWindowFeature(窗口。功能\u无\u标题);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent=getIntent();
String categoryId=intent.getStringExtra(“id”);
JSONParser=新的JSONParser(this);
JSONObject json=parser.getJSONFromAssets(“mylist.json”);
shopItems=新的ArrayList();
试一试{
shops=json.getJSONArray(LIST\u ARR);
对于(int i=0;i
然后,我的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;
}
。
.
.
语境;
列出项目;
私有类视窗持有者{
图像视图图像视图;
TextView-txtName;
TextView-txtId;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
ViewHolder=null;
LayoutInflater充气器=(LayoutInflater)context.getSystemService(Activity.LAYOUT\u充气器\u SERVICE);
if(convertView==null){
convertView=充气机。充气(R.layout.shop\u list\u layout,空);
holder=新的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\U裁剪);
convertView.setTag(支架);
}否则{
holder=(ViewHolder)convertView.getTag();
}
RowItem sItem=(RowItem)getItem(位置);
holder.txtShopName.setText(sItem.getName());
holder.txtCatId.setText(sItem.getCatId());
试一试{
**//我不知道在这里该怎么办
holder.imageView.setImageBitmap(????**
}捕获(例外e){
}
返回视图;
}
然后我有了MyImageLoader类扩展了异步任务
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);
}
}
公共类ShopImageLoader扩展异步任务{
.
.
.
**受保护位图doInBackground(对象…参数){
//如何从其他类获取url?
//然后加载位图(url);
//然后将其设置为dispaly
//我真不知道该在这里做什么。
返回位图??
}**
公共静态位图加载位图(字符串url){
位图=空;
InputStream in=null;
BufferedOutputStream out=null;
试一试{
in=new BufferedInputStream(新URL(URL).openStream(),IO_BUFFER_SIZE);
final ByteArrayOutputStream数据流=新建ByteArrayOutputStream();
out=新的BufferedOutputStream(数据流、IO\U缓冲区大小);
复制(输入、输出);
out.flush();
最后一个字节[]数据=dataStream.toByteArray();
BitmapFactory.Options=new-BitmapFactory.Options();
位图=位图工厂.decodeByteArray(数据,0,数据.length,选项);
}捕获(IOE异常){
Log.e(标记“无法从“+url”加载位图);
}最后{
闭流(in);
闭流(外);
}
返回位图;
}
私有静态void closeStream(可关闭流){
if(流!=null){
试一试{
stream.close();
}捕获(IOE异常){
android.util.Log.e(标记“无法关闭流”,e);
}
}
}
私有静态无效复制(InputStream in,BufferedOutput Stream out)引发IOException{
byte[]byt=新字节[IO_缓冲区大小];
int-read;
而((read=in.read(byt))!=-1){
out.write(byt,0,read);
}
}
好吧,这实际上帮助解决了我的问题。粘贴你的logcat跟踪。由于我无法运行应用程序,我还不能有logcat跟踪。我想我这里的问题是如何使用doinbackground获取我的url,然后将其传递到loadBitmap。然后设置我的位图