list_item.java
public class List_Items extends ListActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);
ListView lv = (ListView) this.findViewById(android.R.id.list);
lv.setAdapter((ListAdapter) new ImageAndTextListAdapter(this, xxx));
Button btn=(Button) findViewById(R.id.button_sync);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
// code here
}
}
ImageAndTextListAdapter.java
public class ImageAndTextListAdapter extends ArrayAdapter<ImageAndText> {
// new method
private ListView listView;
private AsyncImageLoader asyncImageLoader;
//constructor
public ImageAndTextListAdapter(Activity activity, List<ImageAndText> imageAndTexts) {
super(activity, 0, imageAndTexts);
//new method
this.listView = listView;
asyncImageLoader = new AsyncImageLoader();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Activity activity = (Activity) getContext();
// ......
}
}
ImageAndText.java
public class ImageAndText {
private String imageUrl;
private String text;
public ImageAndText(String imageUrl, String text) {
this.imageUrl = imageUrl;
this.text = text;
}
public String getImageUrl() {
return imageUrl;
}
public String getText() {
return text;
}
}
AsyncImageLoader.java
public class AsyncImageLoader {
private HashMap<String, SoftReference<Drawable>> imageCache;
HashMap<String, SoftReference<Drawable>> drawableMap = new HashMap<String, SoftReference<Drawable>>();
public AsyncImageLoader() {
//HashMap<String, SoftReference<Drawable>> drawableMap = new HashMap<String, SoftReference<Drawable>>();
}
public Drawable loadDrawable(final String imageUrl, final ImageCallback imageCallback) {
if (drawableMap.containsKey(imageUrl)) {
SoftReference<Drawable> softReference = imageCache.get(imageUrl);
Drawable drawable = softReference.get();
if (drawable != null) {
return drawable;
}
}
final Handler handler = new Handler() {
@Override
public void handleMessage(Message message) {
imageCallback.imageLoaded((Drawable) message.obj, imageUrl);
}
};
//this is the new thread that download the image from url
new Thread() {
@Override
public void run() {
Drawable drawable = loadImageFromUrl(imageUrl);
imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));
Message message = handler.obtainMessage(0, drawable);
handler.sendMessage(message);
}
}.start();
return null;
}
public static Drawable loadImageFromUrl(String url) {
InputStream inputStream;
try {
inputStream = new URL(url).openStream();
} catch (IOException e) {
throw new RuntimeException(e);
}
return Drawable.createFromStream(inputStream, "src");
}
public interface ImageCallback {
public void imageLoaded(Drawable imageDrawable, String imageUrl);
}
}
В этом примере listview list_item.java изначально пуст, и он будет вызывать ImageAndTextListAdapter, который будет вызывать веб-URL длядинамически предоставлять строку списка данных изображения и текста.
У меня есть вопрос, как вызвать адаптер, lv.setAdapter ((ListAdapter) new ImageAndTextListAdapter (this, xxx));Что должно быть ххх быть?могу ли я просто сделать xxx = List imageAndTexts, какой список класса ImageAndText, но не дублирует ли он то, что находится внутри конструктора ImaheAndTextListAdapter?
Во-вторых, что я должен предоставить в подпрограмме click, public void onClick (View v) {} // code here}} внутри list_item.
Моя цель - нажать кнопку, и это инициирует действие adpater, предоставляющего все необходимые данные.