问题
I have made a custom ArrayAdapter but it is not displaying the different images in each row of the list. It displays only the text but not the images from the server. Here is the code: THE CUSTOM ADAPTER:
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;
}
}
THE DOCTORS CLASS:
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);
}
}
}
THE MAIN CLASS:
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));
}
}
回答1:
Where is your XML file, you have to use an xml file in which you have to specify the contents of listview as ImageView and TextView. and that XML file goes to this line in getView()
in adapter class
(suppose it is list_row.xml)
v=inflater.inflate(R.layout.list_row, parent, false);
来源:https://stackoverflow.com/questions/11645026/a-custom-adapter-that-displays-different-images-from-the-web-in-a-list