简体   繁体   English

如何将ImageView添加到ArrayAdapter?

[英]How can I add an ImageView to an ArrayAdapter?

How do I add ImageViews to an ArrayAdapter to show them in my ListView? 如何将ImageViews添加到ArrayAdapter中以在ListView中显示它们? I tried making the adapter an ArrayAdapter<ImageView> , but it didn't work. 我尝试将适配器设置为ArrayAdapter<ImageView> ,但是没有用。 Here's my code: 这是我的代码:

package com.example.picscroller;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ListView list = (ListView)findViewById(R.id.list);
    ArrayAdapter<ImageView> myarrayAdapter = new ArrayAdapter<ImageView>(this,
    android.R.layout.simple_list_item_1);
    list.setAdapter(myarrayAdapter);
    list.setTextFilterEnabled(true);
    final Button addButton = (Button) findViewById(R.id.addButton);
    final Button removeButton = (Button) findViewById(R.id.removeButton);

    addButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        }
    });
    ImageView droid = new ImageView(this);
    droid.setImageResource(R.drawable.droid);
    myarrayAdapter.add(droid);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }

}

...and here's what happened: ...这是发生了什么: 在此处输入图片说明
I understand why it happened but how can I make it work (how do I add an ImageView to a ListView/ArrayAdapter?) 我知道为什么会发生,但是如何使它起作用(如何将ImageView添加到ListView / ArrayAdapter?)
Thanks. 谢谢。

EDIT Okay, I get that AndroidUniversalImageLoader might be useful but how do I use it @ RSenApps 编辑好吧,我知道AndroidUniversalImageLoader可能很有用,但如何在RSenApps中使用它

我强烈建议您使用https://github.com/nostra13/Android-Universal-Image-Loader而不是自己做,这会使您的生活更加轻松,并且看起来也会更好!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM