I have added many images in the assets folder and want to get those images from there and add it into a ListView. When i am executing the app everything is perfect but the image container of the listview appears blank(transparent). Output
The Java File:
package com.basil.victor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import java.io.IOException;
import java.io.InputStream;
public class Events extends Fragment {
private ListView listEvent;
String eventname[]={
"Name",
"of",
"the",
"events",
"are",
"present",
"here"
};
String eventlogoname[]={
"Logo",
"name",
"of",
"events",
"are",
"present",
"here"
};
Drawable[] arr=new Drawable[7];
String eventsubtitle []={
"Subtitles",
"of",
"the",
"events",
"are",
"present",
"here"
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_events, null);
for(int i=0;i<7;i++) {
try {
InputStream stream = getContext().getAssets().open(eventlogoname[i] + ".jpg");
Drawable el = Drawable.createFromStream(stream, null);
arr[i] = el;
} catch (IOException e) {
e.printStackTrace();
}
}
EventList adapter = new
EventList(getActivity(), eventname, arr, eventsubtitle);
//ListView lv = (ListView)rootView.
listEvent=(ListView)view.findViewById(R.id.listEvent);
listEvent.setAdapter(adapter);
return view;
}
}
ListView adapter:
package com.basil.victor;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class EventList extends ArrayAdapter<String>{
private final Activity context;
private final String[] title;
private final Drawable[] banner;
private final String[] subtitle;
public EventList(Activity context,
String[] title, Drawable[] banner, String[] subtitle) {
super(context, R.layout.list_single, title);
this.context = context;
this.title = title;
this.banner = banner;
this.subtitle = subtitle;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.event_row, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.event_title);
ImageView imageView = (ImageView) rowView.findViewById(R.id.event_banner);
TextView subTitle = (TextView) rowView.findViewById(R.id.event_subtitle);
txtTitle.setText(title[position]);
imageView.setImageDrawable(banner[position]);
subTitle.setText(subtitle[position]);
return rowView;
}
}
If you're getting a
FileNotFound Exception
,Check if the image is inside a subfolder in assets and add the appropriate path
Check the case of the extension (I faced this problem for a long time before I realized my image had a .JPG (Uppercase) extension)
The final string that's being searched, check for random whitespaces