不在水平列表视图中的imageview上显示图像

时间:2014-10-07 11:28:34

标签: android android-imageview baseadapter android-gallery

我试图实现Horizo​​ntal ListView。在这个活动中,我有一个ImageView和一个Horizo​​ntal ListView。如果我从此水平ListView中选择任何图像,该图像将显示在水平ListView上方。默认情况下,第一个图像显示在该ImageView上。此处图像未显示在ImageView上:

String  Imagefile ="http://www.example.com/wp-content/uploads/2014/10/Screenshot_2014-10-07-11-52-52-1412678971.png";
selectedImage = (ImageView) findViewById(R.id.imageView1);
imageLoader=new ImageLoader(DetailPage.this.getApplicationContext());
imageLoader.DisplayImage(Imagefile, selectedImage);

现在,ImageView上的图像显示效果很好。但是,如果我尝试设置动态图像,则图像不会显示在ImageView上。

 GalleryImageAdapter adapter = new GalleryImageAdapter(DetailPage.this, categories);
 gallery.setAdapter(adapter);
 String  Imagefile =categories[0].toString();
 selectedImage = (ImageView) findViewById(R.id.imageView1);
 //imageLoader=new ImageLoader(DetailPage.this.getApplicationContext());
 //imageLoader.DisplayImage(Imagefile, selectedImage);
 gallery.setOnItemClickListener(new OnItemClickListener() {
 public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
 System.out.println("Selected-Image"+" "+categories[position].toString());
  //imageLoader=new ImageLoader(DetailImage.this.getApplicationContext());
  // imageLoader.DisplayImage(categories[position].toString(), selectedImage);

            }
        });  
        }
      class GalleryImageAdapter extends ArrayAdapter<String>
     {
     private final Context context;
      private final String[] categories;
      public ImageLoader imageLoader; 
      public GalleryImageAdapter(Context context, String[] categories) {
        super(context, R.layout.rowlayout, categories);
        this.context = context;
        this.categories = categories;
        imageLoader=new ImageLoader(context.getApplicationContext());
      }

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View i = inflater.inflate(R.layout.rowlayout, parent, false);
        ImageView imageView = (ImageView) i.findViewById(R.id.icon);
       // imageLoader=new ImageLoader(context.getApplicationContext());
        imageLoader.DisplayImage(categories[position], imageView);
        System.out.println("Image"+" "+categories[position]);
        return i;
    }
   }       

但我在logcat上获取网址:

10-07 16:46:35.559: I/System.out(1105): default-Image "http://www.example.com/wp-content/uploads/2014/10/Screenshot_2014-10-07-11-52-52-1412678971.png"
10-07 17:22:50.009: I/System.out(3607): Image http://example.com/wp-content/uploads/2014/10/Screenshot_2014-10-07-11-52-52-1412678971.png

我的代码有什么问题?

修改

是的,我更改了我的代码:

      GalleryImageAdapter adapter = new GalleryImageAdapter(DetailImage.this, R.layout.rowlayout,categories);

      class GalleryImageAdapter extends ArrayAdapter<String>
      {
      private final Context context;
      private final String[] categories;
      public ImageLoader imageLoader;
      private int resourceId; 
      public GalleryImageAdapter(Context context, int resourceId,String[] categories) {
        super(context, R.layout.rowlayout, categories);
        this.context = context;
        this.resourceId = resourceId;
        this.categories = categories;
        imageLoader=new ImageLoader(context.getApplicationContext());
      }

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View i = inflater.inflate(resourceId, parent, false);

但现在仍然没有在ImageView上显示图像。请验证。

修改

我需要具有动态数据的精确输出,如下面的教程:

http://www.learn-android-easily.com/2013/07/android-gallery-view-example.html

6 个答案:

答案 0 :(得分:1)

ImageLoader's

中的构造函数内创建getView(...)对象
public GalleryImageAdapter(Context context, int resourceId, String[] categories) {
  super(context, resourceId, categories);
  this.context = context;
  this.resourceId = resourceId;
  this.categories = categories;
  imageLoader=new ImageLoader(context.getApplicationContext());
}

onItemClick

中注释掉这些行

Activity中为GalleryImageAdapter创建对象的传递布局作为这样的参数

GalleryImageAdapter adapter = new GalleryImageAdapter(DetailPage.this, R.layout.rowlayout, categories);

getView(...)而不是R.layout.rowlayout使用resourceId

P.S: Public Constructors of ArrayAdapter

答案 1 :(得分:1)

好吧,这就是我认为你的影像装载机课上的问题..在你的主动或全球活动中...... 复制并粘贴此代码

DisplayImageOptions options = new DisplayImageOptions.Builder()
    .cacheInMemory()
    .cacheOnDisc()
    .imageScaleType(ImageScaleType.EXACTLY)     
    .build();
    // Load and display image
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(c)
    .defaultDisplayImageOptions(options)
    .build();
    ImageLoader.getInstance().init(config); 

然后在你的数组构造函数中删除并替换 - (这是旧的构造函数 - (你的))

public GalleryImageAdapter(Context context, int resourceId,String[] categories) {
    super(context, R.layout.rowlayout, categories);
    this.context = context;
    this.resourceId = resourceId;
    this.categories = categories;
    imageLoader=new ImageLoader(context.getApplicationContext()); //remove this line
  }

现在你有 - (编辑构造函数)

public GalleryImageAdapter(Context context, int resourceId,String[] categories) {
    super(context, R.layout.rowlayout, categories);
    this.context = context;
    this.resourceId = resourceId;
    this.categories = categories;
  }

然后转到你的getview并执行此操作 - (你可以复制粘贴以替换getview)

@Override
  public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View i = inflater.inflate(R.layout.rowlayout, parent, false);
    ImageView imageView = (ImageView) i.findViewById(R.id.icon);
    ImageLoader.getInstance().displayImage(categories[position], imageView);
    System.out.println("Image"+" "+categories[position]);
    return i;
}

注意:如果未将显示选项传递给ImageLoader.displayImage(...)方法,则将使用配置中的默认显示选项(ImageLoaderConfiguration.defaultDisplayImageOptions(...))。

让我知道是否有帮助

答案 2 :(得分:1)

这是修改后的代码,对我来说很好。因为我不知道你使用了哪个图像加载器我使用了通用图像加载器和一个简单的列表视图。

public class MainActivity extends ActionBarActivity {
ImageView selectedImage;

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

    ListView gallery;
    gallery = (ListView) findViewById(R.id.lv_horiz);
    final String[] categories;
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
            getApplicationContext()).build();
    ImageLoader.getInstance().init(config);
    categories = getResources().getStringArray(R.array.images);
    GalleryImageAdapter adapter = new GalleryImageAdapter(
            getApplicationContext(), getResources().getStringArray(
                    R.array.images));
    gallery.setAdapter(adapter);        
    selectedImage = (ImageView) findViewById(R.id.image);
    gallery.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {
            System.out.println("Selected-Image" + " "
                    + categories[position].toString());
            ImageLoader.getInstance().displayImage(
                    categories[position].toString(), selectedImage);

        }
    });
}

}

适配器类:

class GalleryImageAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] categories;
public ImageLoader imageLoader;

public GalleryImageAdapter(Context context, String[] categories) {
    super(context, R.layout.rowlayout, categories);
    this.context = context;
    this.categories = categories;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);     
    View i = inflater.inflate(R.layout.rowlayout, parent, false);
    ImageView imginlist = (ImageView) i.findViewById(R.id.iv_inlist);
    TextView text = (TextView) i.findViewById(R.id.tv_name);
    ImageLoader.getInstance().displayImage(categories[position], imginlist);
    System.out.println("Image" + " " + categories[position]);
    return i;
}

}

注意:如果您想要从列表视图中查看图像本身时更改图像,则仅在单击列表视图图像(列表视图中的图像)时更改顶部的图像在activity中声明一个静态方法,并从适配器getview中调用它。   PS PM我,如果你需要样本prjoect作为Zip。

答案 3 :(得分:0)

尝试更改此行

View i = inflater.inflate(R.layout.rowlayout, parent, false);

View i = inflater.inflate(R.layout.rowlayout, null);

答案 4 :(得分:0)

在您的代码中,您已注释了用于创建imageloader对象并将该图像设置到selectedImage对象的行。

gallery.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        System.out.println("Selected-Image"+" "+categories[position].toString());
        // imageLoader=new ImageLoader(DetailImage.this.getApplicationContext());
        // imageLoader.DisplayImage(categories[position].toString(), selectedImage);
    }
});

将其更改为:

gallery.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        System.out.println("Selected-Image"+" "+categories[position].toString());
        imageLoader=new ImageLoader(DetailImage.this.getApplicationContext());
        imageLoader.DisplayImage(categories[position].toString(), selectedImage);
    }
});

答案 5 :(得分:0)

您需要使用Volley的Network ImageView

您只需要遵循此tutorial

即可