I am working on a Xamarin Android project in Visual Studio and I am trying to display a picture for each object in a listView, but they are not showing up. When I debug it, it is getting all of the other details of the object, except for the image path and is showing up as null
.
Currently I have the pictures in a images folder inside the project folder. And I listed the path like this
string imageUri = @"C:\Users\aanwar\Documents\Visual Studio 2017\Projects\ContosoLibrary\ContosoLibrary\images";
And it seems to not find the location of the pictures. Should I place the pictures in the drawable folder instead of in the C:\ drive?
class BookListAdapter : BaseAdapter<BookDetailDTO>
{
Activity context;
ImageLoader imageLoader = ImageLoader.Instance;
string imageUri = "C:/Users/aanwar/Documents/Visual Studio 2017/Projects/ContosoLibrary/ContosoLibrary/images";
public BookListAdapter(List<BookDetailDTO> bookList, Activity context) : base()
{
this.context = context;
imageLoader.Init(ImageLoaderConfiguration.CreateDefault(context));
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = items[position];
if (convertView == null)
convertView = context.LayoutInflater.Inflate(Resource.Layout.BookRowView, null);
convertView.FindViewById<TextView>(Resource.Id.bookNameTextView).Text = item.Title;
convertView.FindViewById<TextView>(Resource.Id.priceTextView).Text = "$ " + item.Price;
imageLoader.DisplayImage(imageUri + item.ImagePath + ".jpg",
convertView.FindViewById<ImageView>(Resource.Id.bookImageView));
return convertView;
}
Can someone please assist me in this issue?
And it seems to not find the location of the pictures. Should I place the pictures in the drawable folder instead of in the
C:\
drive?
When running your app on a device/emulator, the app won't have access to your local folder like C:\XXX
.
The correct way to load the Image is to put the image into Resource\drawable
folder and set the property of it to AndroidResource
(by setting this VS will build the image into the final apk
file).:
Then set it to the ImageView
resource using the ResourceId:
ImageView mImageView = FindViewById<ImageView>(Resource.Id.mImageView);
mImageView.SetImageResource(Resource.Drawable.tianyuan);