【问题标题】:Doesn't show the image on imageview in horizontal listview不在水平列表视图中的 imageview 上显示图像
【发布时间】:2014-10-07 11:28:34
【问题描述】:

我尝试实现水平 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 上获得了 url:

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

【问题讨论】:

标签: android android-imageview baseadapter android-gallery


【解决方案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 如果您需要 Zip 示例项目,请 PM 我。

【讨论】:

  • 是的,它工作正常。如果我使用通用图像加载器库。谢谢:)
  • 欢迎您@KrishnaVeni :) (y)
【解决方案2】:

在构造函数中创建 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

附注: Public Constructors of ArrayAdapter

【讨论】:

  • 是的,我已经像你的逻辑一样改变了..但是图像没有显示。
  • 我已经更新了我的代码。但我没有得到解决方案。
【解决方案3】:

好的,这就是我认为问题出在您的图像加载器类上的原因。在您的主要活动或全局活动中尝试此操作... 复制并粘贴这段代码

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(...))。

如果有帮助请告诉我

【讨论】:

  • 那么为什么我没有获得 50 赏金声誉? @埃尔茨?她必须手动提供给我吗? :D 哈哈
  • LOL...是的,她必须手动提供,但她在赏金期间接受了你的回答,这将使你在宽限期后4小时内获得赏金more info about bounty jux btw@ Vigneshearan.m
  • 感谢您提供的信息@Elltz :)
【解决方案4】:

尝试更改此行

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

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

【讨论】:

    【解决方案5】:

    在您的代码中,您已经注释了用于创建 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);
        }
    });
    

    【讨论】:

      【解决方案6】:

      需要使用 Volley 的 Network ImageView

      你只需要关注这个tutorial

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-01
        • 2014-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多