我试图实现Horizontal ListView。在本活动中,我有一个ImageView和一个Horizontal 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
这是修改后的代码,对我来说很好用,因为我不知道您使用了哪个图像加载器,所以我使用了通用图像加载器并具有简单的列表视图。
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;
}
}
注意:仅当单击列表视图图像(列表视图中的图像)时,如果要更改图像(从列表视图中查看的图像本身只是在活动内部声明一个静态方法并调用它),这只会更改顶部的图像从适配器的getview。PS PM我,如果您需要将样本作为Zip。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句