サーバから返されていない場合は何も画像がサーバーからダウンロードされていないときの目標表示デフォルトの画像なし画像は
は、デフォルトの画像を表示します。
問題
私は(いくつかのテキストボックスと一緒にしかし重要ではないthatsの)ImageViewのとリストビューを持っています。 私の画像ビューは生徒のための画像をダウンロードしますが、生徒に画像がないときは、デフォルトの画像を表示しようとしています。 私は、動作するはずの2つのことを試したり、デフォルトのイメージを設定したり、以下のコードを試しました。 私はimgv = (ImageView)findViewById(R.id.ivImage);
にヌルrefernceを取得していますし、私はなぜわからないが、このコードは、私は、変数にデータベースの列から値を書き込むアクティビティファイル(のみシンプルさを維持するための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");
}
から取られます私のイメージビューが宣言されているからです。 カラムから が供給されていない場合のデフォルトイメージの効果を理解するのに役立ちます。
もう少し具体的には、上記のコードは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>
行
<?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>
アダプタ呼び出す一覧:
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);
}
}
}
あなたのXMLコードを投稿してください。 –
はあなたのアダプタクラスのコードの上にあります..? –
xmlとログをポストします。もう1つのことは、上記のfindViewbyId行をelse条件で宣言しなければならないことです。 – Pankaj