我创建了一个程序,它从服务器读取文件以构建列表视图。然后它在后台加载图像。现在,图像被加载到imageview中以确保它正常工作。如何将图像加载到listview imageview行?
public class Welcome extends ListActivity {
/** Called when the activity is first created. */
// data to load from html file
ArrayList<String> images;
ArrayList<String> Links;
ArrayList<String> LinkName;
ArrayList<String> Price;
int y;
ArrayList<String> ted;
int i;
String[] items={"CASUAL","DRESSES","CAREER","OUTWEAR","FOOTWEAR",
"JEWELRY","ACCESSORIES"};
private HttpClient client;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// data read in can be from 1-10
images=new ArrayList();
Links=new ArrayList();
LinkName=new ArrayList();
Price=new ArrayList();
// load in arrays from a file on a web server
Loaddata();
// create a listvue based on the data loaded from the server
setListAdapter(new IconicAdapter(this));
// load in biytmap
download();
} // end function
class IconicAdapter extends ArrayAdapter {
Activity context;
IconicAdapter(Activity context) {
super(context, R.layout.row, LinkName);
this.context=context;
}
public View getView(int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View row=inflater.inflate(R.layout.row, null);
TextView label=(TextView)row.findViewById(R.id.label);
label.setText( LinkName.get(position) );
return(row);
}
}
private void download(){
new AsyncTask<Void, Void, Bitmap>(){
@Override
protected Bitmap doInBackground(Void... params) {
HttpClient client = new DefaultHttpClient();
try {
String uri = "http://www.besttechsolutions.biz/icon.png";
HttpGet request = new HttpGet(uri);
HttpResponse response = client.execute(request);
return BitmapFactory.decodeStream(response.getEntity().getContent());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap image) {
if(image == null){
Log.d("ted", "could not download image");
// Toast.makeText(Main.this, "Download failed", Toast.LENGTH_LONG).show();
}
else{
// losd into ListView
// ImageView myimage = (ImageView) findViewById(R.id.myimage);
// myimage.setImageBitmap(image);
}
}
}.execute();
}
答案 0 :(得分:0)
if(image == null){
Log.d("ted", "could not download image");
getAdapter().setImage(image);
//refresh
getAdapter().notifyDataSetChanged();
}
并将setImage
方法添加到IconicAdapter
:
public void setImage(image){
this.image = image;
}
您的适配器中需要一个字段image
,您应该在getView
中签入,并ImageView
设置image!=null
以防{{1}}。