我制作了一个自定义 ArrayAdapter,但它没有在列表的每一行中显示不同的图像。它只显示文本而不显示来自服务器的图像。这是代码:自定义适配器:
package com.findadoc;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class doctorslistAdapter extends ArrayAdapter<Doctors> {
Context context;
int layoutResource;
Doctors[]objects=null;
public doctorslistAdapter(Context context, int resource, Doctors[] objects) {
super(context, resource, objects);
this.context=context;
this.layoutResource=resource;
this.objects=objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v=convertView;
DoctorsHolder holder;
if(v==null){
LayoutInflater inflater=((Activity)context).getLayoutInflater();
v=inflater.inflate(layoutResource, parent, false);
holder=new DoctorsHolder();
holder.doc_icon=(ImageView)v.findViewById(R.id.imageViewdocimg);
holder.doc_name=(TextView)v.findViewById(R.id.textViewdocname);
holder.specialization=(TextView)v.findViewById(R.id.textViewspecialization);
v.setTag(holder);
}
else
{
holder=(DoctorsHolder)v.getTag();
}
Doctors doc=objects[position];
holder.doc_icon.setImageBitmap(doc.bitmap);
holder.doc_name.setText(doc.doc_name);
holder.specialization.setText(doc.specialization);
return v;
}
static class DoctorsHolder{
ImageView doc_icon;
TextView doc_name,specialization;
}
}
医生班:
package com.findadoc;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
public class Doctors {
public String doc_name,specialization;
public Bitmap bitmap;
public Doctors(String icon,String doc_name,String specialization){
this.doc_name=doc_name;
this.specialization=specialization;
(new bitmapclass(icon)).execute();
}
class bitmapclass extends AsyncTask<Void, Void, Void>{
public String icon_url;
public Bitmap bm = null;
public bitmapclass(String icon_url){
this.icon_url=icon_url;
}
@Override
protected Void doInBackground(Void... arg0) {
try {
URL aURL = new URL("http://10.0.2.2/findadoc/images/"+this.icon_url);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is, conn.getContentLength());
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
Doctors.this.bitmap=bm;
super.onPostExecute(result);
}
}
}
主要课程:
package com.findadoc;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import com.findadoc.Doctors.bitmapclass;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.ListView;
public class doctorsActivity extends Activity {
ListView listviewdocs;
ImageView imageview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.doctorsactivity);
imageview=(ImageView)findViewById(R.id.imageViewtest);
Doctors d=new Doctors("doc1.png", "", "");
imageview.setImageBitmap(d.bitmap);
listviewdocs=(ListView)findViewById(R.id.listViewDoctors);
Doctors doctors_data[]=new Doctors[]{
new Doctors("doc1.png", "Dr. Sammy Kulova","Gyaenacologist"),
new Doctors("doc2.png","Dr.Joan Kulova","Paediatrician"),
new Doctors("doc3.png","Dr. John Kulova","Pharmacist")
};
listviewdocs.setAdapter(new doctorslistAdapter(doctorsActivity.this, R.layout.doctors_list_item_row, doctors_data));
}
}