目标
没有从服务器下载图像时显示默认图像.
问题
我有一个带有图像视图的列表视图(以及一些文本框,但这并不重要).我的imageview为学生下载图像,但是当学生没有图像时,我试图显示默认图像.我尝试了两件我认为应该工作的东西,设置了一个默认图像,或者下面的代码.此代码取自活动文件,我将数据库列中的值写入变量(仅显示img以保持简单性)
//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");
}
Run Code Online (Sandbox Code Playgroud)
但是我得到了一个空参考imgv = (ImageView)findViewById(R.id.ivImage);,我不知道为什么我的图像视图被声明.如果没有从列提供默认图像的任何帮助,我们将不胜感激.
对于更多上下文,上面的代码是一个调用listview.xml的活动,然后调用row.xml.有问题的imageview在row.xml文件中.
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>
Run Code Online (Sandbox Code Playgroud)
调用行的列表
<?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>
Run Code Online (Sandbox Code Playgroud)
适配器:
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
hum*_*zed 17
有一种更好的方法,你可以使用一个图像加载库,如:
滑翔.
适用于Android的图像加载和缓存库专注于平滑滚动
只需要一行即可完成你想要的任务.
Glide.with(myFragment)
.load(url)
.placeholder(R.drawable.loading_spinner)
.into(myImageView);
Run Code Online (Sandbox Code Playgroud)
它更容易,更清洁.
小智 1
此方法可行 - 在 xml 代码中设置默认图像,当学生没有图像时将显示该默认图像。如果您有图像,请使用 setImageResource(...) 动态设置图像。
当您收到空引用错误时,请重新检查 ImageView 的 id,如果这不能解决问题,请发布完整的活动代码。
| 归档时间: |
|
| 查看次数: |
28910 次 |
| 最近记录: |