gpt4 book ai didi

android - 从 JSON URL 加载图像到 ListView

转载 作者:行者123 更新时间:2023-11-29 15:25:32 24 4
gpt4 key购买 nike

我正在尝试从 URL 中获取图像,然后将其添加到 ListView 中。我可以成功获取图像,但我很难将其添加到 ListView 。

我试过这种方式:

    ListView lv = (ListView) findViewById(R.id.list);

try {

URL url = new URL("http://devcms.barcodo.com/Images/ProductImages/ThumbnailImages100/EG-BIRT-ST-JA_th.jpg");
HttpGet httpRequest = null;
httpRequest = new HttpGet(url.toURI());
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
InputStream input = b_entity.getContent();
Bitmap bitmap = BitmapFactory.decodeStream(input);

HashMap<String, Object> docList = new HashMap<String, Object>();
docList.put("IMAGE", bitmap);

ArrayList<HashMap<String, Object>> al = new ArrayList<HashMap<String, Object>>();
al.add(docList);

simpleAdapter = new SimpleAdapter(this, al, R.layout.list_item,new String[] { "IMAGE" }, new int[] { R.id.image});
lv.setAdapter(simpleAdapter);

注意:我可以将其放入 ImageView,但想获取到 ListView 。

最佳答案

尝试以下操作:

         public class ImageURL extends ListActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
try {

URL url = new URL(
"http://devcms.barcodo.com/Images/ProductImages/ThumbnailImages100/EG-BIRT-ST-JA_th.jpg");
HttpGet httpRequest = null;

httpRequest = new HttpGet(url.toURI());

HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient
.execute(httpRequest);

HttpEntity entity = response.getEntity();
BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
InputStream input = b_entity.getContent();

bitmap = BitmapFactory.decodeStream(input);

} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();

} catch (MalformedURLException e) {
Log.e("log", "bad url");
} catch (IOException e) {
Log.e("log", "io error");
}
setListAdapter(new StudentListAdapter(this));
}

private class StudentListAdapter extends BaseAdapter {
private Context mContext;
private String[] mStudents = { "DurgaPrasad", "Raghu", "Vivek",
"Satish", "Naga Jyothi", "Vardhika", "Nikhil" };
private String[] mDetailsStudent = { "Details of DurgaPrasad",
"Details of Raghu This row is not created using java",
"Details of Vivek", "Details of Satish",
"Details of Naga Jyothi", "Details of Vardhika",
"Details of Nikhil" };

public StudentListAdapter(Context context) {
mContext = context;
}

public int getCount() {
return mStudents.length;
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
System.out.println("111111111111 : " + position);
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

/*
* if (position == 0) {
* System.out.println("111111111111 : "+position); v =
* vi.inflate(R.layout.studentdetailsrow, null);
* System.out.println("111111111111 : "+position); } else
*/
v = vi.inflate(R.layout.list_item, null);
}

ImageView iv = (ImageView) v.findViewById(R.id.icon);
ImageView iv2 = (ImageView) v.findViewById(R.id.icon2);
if (position == 0) {
iv.setImageBitmap(bitmap);
// iv2.setImageResource(R.drawable.icon);
} else {
iv.setImageBitmap(bitmap);
// iv2.setImageResource(R.drawable.icon);
}

TextView tvname = (TextView) v.findViewById(R.id.stuname);
TextView tvdetail = (TextView) v.findViewById(R.id.studetail);
tvname.setText(mStudents[position]);
tvdetail.setText(mDetailsStudent[position]);
return v;
}

};
}

关于android - 从 JSON URL 加载图像到 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13698135/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com