goal
Display the default image when the image does not load from the server.
Problem
I have a listview with an image view (along with several text fields, but that doesn't matter). My image is viewing images for students, but when the student has no image, I try to display the default image. I tried two things that I thought should work, set the default image or the code below. This code is taken from an activity file where I write values ββfrom database columns to variables (only to show img simplicity)
//Image path returned if (javaRealObject.getString("img").equals("")) { imgv = (ImageView)findViewById(R.id.ivImage); imgv.setImageResource(R.drawable.emptyhead); Log.d("Test", "Empty"); } else//No image found in column { student.setImage(javaRealObject.getString("img")); Log.d("Test","Not Empty"); }
However, I get a null refernce on imgv = (ImageView)findViewById(R.id.ivImage); , and I'm not sure why since the announcement of my image. Any help can affect the default image, when none of them is delivered from the column will be appreciated.
For a bit more context, the above code is an action that calls listview.xml, which then calls row.xml. the image in question is in the row.xml file.
Row.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="4dp" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <ImageView android:id="@+id/ivImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="8dp" android:src="@drawable/empty_head" /> //default image here <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/tvFirstName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/primary" android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout> </LinearLayout> </LinearLayout>
List calling string
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="450dp" tools:listitem="@layout/row" > </ListView> </LinearLayout>
Adapter:
public class DriverAdapter extends ArrayAdapter<Drivers> { ArrayList<Drivers> ArrayListDrivers; int Resource; Context context; LayoutInflater vi; public DriverAdapter(Context context, int resource, ArrayList<Drivers> objects) { super(context, resource, objects); ArrayListDrivers = objects; Resource = resource; this.context = context; vi = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = vi.inflate(Resource, null); holder = new ViewHolder(); holder.imageview = (ImageView) convertView.findViewById(R.id.ivImage); holder.tvName = (TextView) convertView.findViewById(R.id.tvFirstName); holder.tvDescription = (TextView) convertView.findViewById(R.id.tvLastName); holder.tvClientid = (TextView) convertView.findViewById(R.id.tvid); holder.tvExpires = (TextView) convertView.findViewById(R.id.tv_expdate); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.imageview.setImageResource(R.drawable.ic_launcher); new DownloadImageTask(holder.imageview).execute(ArrayListDrivers.get(position).getImage()); Glide.with(holder.imageview.getContext()) .load(new DownloadImageTask(holder.imageview).execute(ArrayListDrivers.get(position).getImage()) ) .centerCrop() .placeholder(R.drawable.ic_launcher) .crossFade() .into(holder.imageview); holder.tvName.setText(ArrayListDrivers.get(position).getFirstname()); holder.tvDescription.setText(ArrayListDrivers.get(position).getLastname()); holder.tvClientid.setText(ArrayListDrivers.get(position).getClient_id()); holder.tvExpires.setText("Expiry Date:"+ArrayListDrivers.get(position).getExpires()); return convertView; } static class ViewHolder { public ImageView imageview; public TextView tvName; public TextView tvDescription; public TextView tvClientid; public TextView tvExpires; } private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { ImageView bmImage; public DownloadImageTask(ImageView bmImage) { this.bmImage = bmImage; } protected Bitmap doInBackground(String... urls) { String urldisplay = urls[0]; Bitmap cImg1 = null; try { byte[] decodedString = Base64.decode(urldisplay, Base64.DEFAULT); cImg1 = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); } catch (Exception e) { Log.e("Error", e.getMessage()); e.printStackTrace(); } return cImg1; } protected void onPostExecute(Bitmap result) { bmImage.setImageBitmap(result); } } }