在我的gridView中,我想从url加载图像,因为我使用了ImageLoader库,,,,我使用了PhotoAdapter.class来设置adter到gridview,并且我已经保存了PhotoGallery.class中的所有URL。
但我的gridview显示空白。
GalleryViewActivity.class:
public class GalleryViewActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.photoview);
initViews(); }
public void initViews() {
GridView gridView = (GridView) findViewById(R.id.photoview);
gridView.setAdapter(new PhotosAdapter(GalleryViewActivity.this,
(List<PhotoGallery>) gridView));
}
}
PhotoAdapter.class
public class PhotosAdapter extends BaseAdapter {
class PhotosViewHolder {
ImageView imageView;
public PhotosViewHolder(View convertView) {
imageView = (ImageView) convertView.findViewById(R.id.images_child);
}
public void loadImageUrls(ImageLoader imageLoader, String url) {
imageLoader.displayImage(url, imageView);
}
}
private Activity activity;
private ImageLoader imageLoader;
private List<PhotoGallery> photoGalleries;
public PhotosAdapter(Activity activity, List<PhotoGallery> galleries) {
this.activity = activity;
this.photoGalleries = galleries;
this.imageLoader = new ImageLoader(activity);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return photoGalleries.size();
}
@Override
public PhotoGallery getItem(int position) {
// TODO Auto-generated method stub
return photoGalleries.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
PhotosViewHolder viewHolder;
if (convertView == null) {
convertView = View.inflate(activity, R.layout.gallery_child_layout,
null);
viewHolder = new PhotosViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (PhotosViewHolder) convertView.getTag();
}
PhotoGallery gallery = getItem(position);
viewHolder.loadImageUrls(imageLoader, gallery.getImageUrl());
return convertView;
}
}
PhotoGallery.class:
public class PhotoGallery {
private String imageUrl;
public PhotoGallery(int i) {
int index = i % 3;
imageUrl = urls[index];
}
private String urls[] = {
"http://mobileapp.certifytechnologies.com/jumeirah/ju_gal_img/gal_1.jpg",
"http://mobileapp.certifytechnologies.com/jumeirah/ju_gal_img/gal_2.jpg",
"http://mobileapp.certifytechnologies.com/jumeirah/ju_gal_img/gal_3.jpg",
"http://mobileapp.certifytechnologies.com/jumeirah/ju_gal_img/gal_4.jpg",
"http://mobileapp.certifytechnologies.com/jumeirah/ju_gal_img/gal_5.jpg",
"http://mobileapp.certifytechnologies.com/jumeirah/ju_gal_img/gal_6.jpg",
"http://mobileapp.certifytechnologies.com/jumeirah/ju_gal_img/gal_7.jpg",
"http://mobileapp.certifytechnologies.com/jumeirah/ju_gal_img/gal_8.jpg",
"http://mobileapp.certifytechnologies.com/jumeirah/ju_gal_img/gal_9.jpg",
"http://mobileapp.certifytechnologies.com/jumeirah/ju_gal_img/gal_10.jpg",
"http://mobileapp.certifytechnologies.com/jumeirah/ju_gal_img/gal_11.jpg",
"http://mobileapp.certifytechnologies.com/jumeirah/ju_gal_img/gal_12.jpg",
"http://mobileapp.certifytechnologies.com/jumeirah/ju_gal_img/gal_13.jpg",
"http://mobileapp.certifytechnologies.com/jumeirah/ju_gal_img/gal_14.jpg",
"http://mobileapp.certifytechnologies.com/jumeirah/ju_gal_img/gal_15.jpg",
"http://mobileapp.certifytechnologies.com/jumeirah/ju_gal_img/gal_16.jpg",
"http://mobileapp.certifytechnologies.com/jumeirah/ju_gal_img/gal_17.jpg",
"http://mobileapp.certifytechnologies.com/jumeirah/ju_gal_img/gal_18.jpg",
"http://mobileapp.certifytechnologies.com/jumeirah/ju_gal_img/gal_19.jpg",
"http://mobileapp.certifytechnologies.com/jumeirah/ju_gal_img/gal_20.jpg",
"http://mobileapp.certifytechnologies.com/jumeirah/ju_gal_img/gal_21.jpg",
"http://mobileapp.certifytechnologies.com/jumeirah/ju_gal_img/gal_22.jpg",
"http://mobileapp.certifytechnologies.com/jumeirah/ju_gal_img/gal_23.jpg",
"http://mobileapp.certifytechnologies.com/jumeirah/ju_gal_img/gal_24.jpg" };
public String getImageUrl() {
return imageUrl;
}
}
photoview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/splashscreen"
android:orientation="vertical" >
<GridView
android:id="@+id/photoview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#A3000000"
android:horizontalSpacing="1dp"
android:numColumns="3"
android:paddingTop="20dp"
android:stretchMode="columnWidth"
android:verticalSpacing="15dp" />
</LinearLayout>
gallery_child_view.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/images_child"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/splashscreen"
android:scaleType="fitXY"/>
</LinearLayout>
答案 0 :(得分:0)
您是否已向该应用授予了Internet权限?在AndroidManifest.xml中
<manifest xlmns:android...>
...
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
答案 1 :(得分:0)
调用在onCreate()
之后创建的initViews()方法解决方案:
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.photoview);
initViews();
}
答案 2 :(得分:0)
在您的GalleryViewActivity.class中,您似乎无法在onCreate方法上调用initViews(),因此您的网格永远不会被初始化。
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.photoview);
// Add this one here.
initViews();
}
作为额外的建议,您不应该保存对主要活动的引用。如果你想要扩展布局,你应该使用getContext()(或getActivity()。getContext())而不是保存引用。
这部分代码完全错误:
gridView.setAdapter(new PhotosAdapter(GalleryViewActivity.this,
(List<PhotoGallery>) gridView));
我建议您阅读GridView上的android文档以及Java Collections的一些主题,因为您无法将GridView强制转换为对象列表。 如果你不了解基础知识,那么你就很难理解你想要达到的目标。