我剛開始一些基本的Android編程,但我有點卡住了。儘可能簡單,我想創建一個包含圖像的列表視圖(最終產品是一個排序庫,全屏,沒有文本)。我嘗試從URL中加載同步的圖像,然後將它們加載到我的列表視圖中。真正發生的是,我的listview被文本填充(android.widget.id blabla)。它似乎是我的圖像瀏覽的標識,因此我認爲問題在於適配器。爲圖像創建自定義適配器可以解決問題,該適配器將如何看起來像?我試圖尋找出一些例子(因爲很多人都嘗試過類似的東西),但我無法找到適合的Imageview和listviews - 是自定義適配器的解決方案嗎?
MainActitivy.java:
public class MainActivity extends Activity {
private ArrayList<ImageView> titles;
private ArrayAdapter<ImageView> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
titles = new ArrayList<ImageView>();
final ListView listview = (ListView) findViewById(R.id.list);
adapter = new ArrayAdapter<ImageView>(this, android.R.layout.simple_list_item_1, android.R.id.text1, titles);
listview.setAdapter(adapter);
titles.add(new ImageView(this));
loadItems("http://test.com/images/3da25cead4806472c8c27012f724be45d0022cc7.jpeg");
loadItems("http://test.com/images/fbef951ceb9a83d86427d607d67f1ba93008bdfa.jpeg");
loadItems("http://test.com/images/e982478695ab765bb746dce4da8328a60fbd0bf6.jpeg");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void loadItems(String url){
new LoadImage((ImageView) new ImageView(this), this).execute(url);
//adapter.notifyDataSetChanged();
}
public void addToList(ImageView image){
titles.add(image);
adapter.notifyDataSetChanged();
}
}
LoadImage.java :
class LoadImage extends AsyncTask<String, Void, Bitmap> {
private ImageView image;
private MainActivity mainActivity;
public LoadImage(ImageView image, MainActivity mainActivity) {
this.image = image;
this.mainActivity = mainActivity;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap image = null;
try{
InputStream in = new java.net.URL(urldisplay).openStream();
image = BitmapFactory.decodeStream(in);
}catch(Exception e){
Log.e("Error: ", e.getMessage());
}
return image;
}
protected void onPostExecute(Bitmap result) {
image.setImageBitmap(result);
mainActivity.addToList(image);
}
}
編輯: 所以,我嘗試使用通用圖像裝載機的建議,我也嘗試創建自己的適配器。下面是它看起來像現在:
MainActivity.java:
public class MainActivity extends Activity {
private ImageLoader imageLoader;
private List<ImageObject> images;
private CustomAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
images = new ArrayList<ImageObject>();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this.getBaseContext()).build();
imageLoader = ImageLoader.getInstance();
imageLoader.init(config);
adapter = new CustomAdapter(this, R.layout.list_view_row_item, images);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
addImage("https://www.google.dk/images/srpr/logo11w.png");
addImage("https://www.google.dk/images/srpr/logo11w.png");
addImage("https://www.google.dk/images/srpr/logo11w.png");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void addImage(String url){
ImageObject image = new ImageObject(url, imageLoader);
images.add(image);
adapter.notifyDataSetChanged(); //Is this legit??
}
}
CustomAdapter.java:
public class CustomAdapter extends ArrayAdapter<ImageObject>{
private List<ImageObject> images;
private Context context;
private int resourceID;
public CustomAdapter(Context context, int resourceID, List<ImageObject> images) {
super(context, resourceID, images);
this.context = context;
this.images = images;
this.resourceID = resourceID;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if(view == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(resourceID, null);
}
ImageObject image = images.get(position);
if(image != null){
// What goes here?
}
return view;
}
}
ImageObject.java:
public class ImageObject {
private String url;
private ImageView image;
public ImageObject(String url, ImageLoader imageLoader){
this.setUrl(url);
imageLoader.displayImage(url, image);
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public ImageView getImage() {
return image;
}
public void setImage(ImageView image) {
this.image = image;
}
}
我怎麼把我的loa代表圖像到列表元素? (//這裏有什麼?)
再次編輯: 通過將通用加載器代碼更改爲適配器來解決此問題。 ImageView imageV =(ImageView)view.findViewById(R.id.imageview); imageLoader.displayImage(image.getUrl(),imageV);
請了投我的答案。如果它幫助你。 –