Reputation: 5413
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);
}
}
In this example, the listview of the list_item.java is initially empty, and it will call the ImageAndTextListAdapter which will call the web url to supply the listview row of data of image and Text dynamically.
The question I have is how to call the Adapter, lv.setAdapter((ListAdapter) new ImageAndTextListAdapter(this, xxx)); What should be xxx be? can I just do a xxx=List imageAndTexts which list of the ImageAndText class but isn't that duplicate what's inside the ImaheAndTextListAdapter constructor?
Secondly, what should I supply inside the click routine, public void onClick(View v) {
}
// code here
}
} inside the list_item.
The goal of mine is hit the button and that will initiate the action of adpater supplying all the necessary data.
Upvotes: 0
Views: 1122
Reputation: 26971
From the ImageAndTextListAdapter adapter the xxx should be a List.
Your adapter takes two parameters of a Activity, and List.
So you should create a List, and create objects of ImageAndText class to add to the list like this..
ImageAndText image = new ImageAndText("url","Test");
List<ImageAndText>text;
text.add(image); //Add the Object of ImageAndText
ListView lv = (ListView) findViewById(android.R.id.list);
//Here i supply the adapter with the text list created.
lv.setAdapter((ListAdapter)new ImageAndTextListAdapter(Main.this, text));
Upvotes: 1