我没有做太多的java和android,并且卡在将图像加载到listview中。希望我能通过使用BaseAdapter来处理正确的事情。现在我只尝试一个图像,但最终它将是一个图像列表。尝试了一些教程,但没有运气。
我正在使用Andorid-Universal-Image-Downloader(https://github.com/nostra13/Android-Universal-Image-Loader)来下载图像。增加了互联网和存储的许可。仍然没有任何事情发生,只是一个空白的屏幕,这是我的代码:
ImageAdapter:
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;
}
}
MainActivity:
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>
答案 0 :(得分:0)
您的getCount
返回0,因此ListView不会从适配器请求任何视图。尝试返回1 - 可能有帮助。