我没有做太多的 java 和 android 并且被困在将图像加载到列表视图中。希望我通过使用 BaseAdapter 走在正确的轨道上。现在我只尝试一个图像,但最终它将是一个图像列表。尝试遵循一些教程,但没有运气。
我正在使用 Andorid-Universal-Image-Downloader(https://github.com/nostra13/Android-Universal-Image-Loader)下载图像。添加了互联网和存储权限。仍然没有发生任何事情,只是一个空白屏幕,这是我的代码:
图像适配器:
public class ImageAdapter extends BaseAdapter {
private final Context context;
ImageLoader imageLoader = ImageLoader.getInstance();
Bitmap image;
public ImageAdapter(Context c) {
this.context = c;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.row, parent, false);
ImageView imageView = (ImageView) row.findViewById(R.id.imageView);
imageLoader.init(ImageLoaderConfiguration.createDefault(context));
imageLoader.loadImage("urlToImage", new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
image = loadedImage;
}
});
imageView.setImageBitmap(image);
return row;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
主要活动:
public class MainActivity extends Activity {
ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.listView);
ImageAdapter adapter = new ImageAdapter(this);
listView.setAdapter(adapter);
}
}
xml:
----main.xml----
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView" />
</LinearLayout>
----row.xml----
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:minHeight="64dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="9dp"
android:layout_alignParentTop="true"/>
</RelativeLayout>