I have not done much java and android and got stuck in loading an image into a listview. Hopefully I'm on the right track of something by using a BaseAdapter. Now I am trying only one image, but eventually it will be a list of images. Tried following some tutorials but no luck.
I am using Andorid-Universal-Image-Downloader(https://github.com/nostra13/Android-Universal-Image-Loader) to download the images. Permission for internet and storage is added. Still there is nothing happening, just a blank screen, here is my code:
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>