我想从URL图像,然后将其添加到列表视图。我可以成功地得到图像,但我挣扎着将它添加到列表视图。
I am trying to get image from URL then add it to a listview. I can successfully get the image, but I'm struggling with adding it to the listview.
我已经试过这样:
ListView lv = (ListView) findViewById(R.id.list);
try {
URL url = new URL("https://m.xsw88.com/allimgs/daicuo/20230905/2115.png.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
Note: I am able to put it into an ImageView, but want to fetch into a 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(
"https://m.xsw88.com/allimgs/daicuo/20230905/2115.png.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;
}
};
}