リサイクラービューでユーザーの詳細を表示しようとしています。AndroidでjsonのURLから画像をダウンロードして表示する
私の画像を表示しようとすると動作しません。私はすべての可能性を試しました。以下は私のアダプタクラスです。
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{
private LinkedList<HashMap<String, Object>> myDataSet;
Bitmap bitmap;
MyAdapter.ViewHolder viewHolder;
String image;
public MyAdapter(LinkedList<HashMap<String, Object>> values) {
myDataSet = values;
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView userIdView;
public TextView jobTitleNameView;
public TextView employeeCodeView;
public ImageView imageView;
public ViewHolder(View itemView) {
super(itemView);
userIdView = (TextView)itemView.findViewById(R.id.userId);
jobTitleNameView = (TextView)itemView.findViewById(R.id.jobTitleName);
employeeCodeView = (TextView)itemView.findViewById(R.id.employeeCode);
}
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(
parent.getContext());
View v = inflater.inflate(R.layout.activity_listview, parent, false);
viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final HashMap<String, Object> userDetails = myDataSet.get(position);
String userId = (String) userDetails.get("userId");
String jobTitleName = (String) userDetails.get("jobTitleName");
String employeeCode = String.valueOf(userDetails.get("employeeCode"));
image = (String) userDetails.get("image");
ImageDownloader imageDownloader = new ImageDownloader();
imageDownloader.execute(viewHolder);
holder.userIdView.setText(userId);
holder.jobTitleNameView.setText(jobTitleName);
holder.employeeCodeView.setText(employeeCode);
}
@Override
public int getItemCount() {
return myDataSet.size();
}
これはイメージをダウンロードするためにこのクラスの内部に書き込まれるクラスです。
private class ImageDownloader extends AsyncTask<ViewHolder,ViewHolder,ViewHolder>{
@Override
protected ViewHolder doInBackground(ViewHolder... params) {
viewHolder = params[0];
URL url = null;
try {
url = new URL(image);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream inputStream = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
return viewHolder;
}
@Override
protected void onPostExecute(ViewHolder result){
viewHolder.imageView.setImageBitmap(bitmap);
}
}
}
そのない直接的な解決策は、私はピカソやグライドのようなライブラリを使用することをお勧めします可能性がありますが、誰かが
03-24 16:27:48.421 19806-20764/com.example.rini.listviewandroid W/System.err: java.net.ConnectException: failed to connect to www.scri8e.com/208.87.227.250 (port 80): connect failed: ETIMEDOUT (Connection timed out)
03-24 16:27:48.421 19806-20764/com.example.rini.listviewandroid W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:124)
03-24 16:27:48.421 19806-20764/com.example.rini.listviewandroid W/System.err: at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
03-24 16:27:48.421 19806-20764/com.example.rini.listviewandroid W/System.err: at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
03-24 16:27:48.421 19806-20764/com.example.rini.listviewandroid W/System.err: at java.net.Socket.connect(Socket.java:882)
03-24 16:27:48.421 19806-20764/com.example.rini.listviewandroid W/System.err: at com.android.okhttp.internal.Platform.connectSocket(Platform.java:174)
03-24 16:27:48.422 19806-20764/com.example.rini.listviewandroid W/System.err: at com.android.okhttp.Connection.connect(Connection.java:152)
03-24 16:27:48.422 19806-20764/com.example.rini.listviewandroid W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:276)
03-24 16:27:48.422 19806-20764/com.example.rini.listviewandroid W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
03-24 16:27:48.422 19806-20764/com.example.rini.listviewandroid W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
03-24 16:27:48.422 19806-20764/com.example.rini.listviewandroid W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
03-24 16:27:48.422 19806-20764/com.example.rini.listviewandroid W/System.err: at com.example.rini.listviewandroid.MyAdapter$ImageDownloader.doInBackground(MyAdapter.java:94)
03-24 16:27:48.422 19806-20764/com.example.rini.listviewandroid W/System.err: at com.example.rini.listviewandroid.MyAdapter$ImageDownloader.doInBackground(MyAdapter.java:77)
03-24 16:27:48.422 19806-20764/com.example.rini.listviewandroid W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:292)
03-24 16:27:48.422 19806-20764/com.example.rini.listviewandroid W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-24 16:27:48.422 19806-20764/com.example.rini.listviewandroid W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
03-24 16:27:48.422 19806-20764/com.example.rini.listviewandroid W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-24 16:27:48.422 19806-20764/com.example.rini.listviewandroid W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-24 16:27:48.422 19806-20764/com.example.rini.listviewandroid W/System.err: at java.lang.Thread.run(Thread.java:818)
03-24 16:27:48.422 19806-20764/com.example.rini.listviewandroid W/System.err: Caused by: android.system.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
03-24 16:27:48.422 19806-20764/com.example.rini.listviewandroid W/System.err: at libcore.io.Posix.connect(Native Method)
03-24 16:27:48.422 19806-20764/com.example.rini.listviewandroid W/System.err: at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:111)
03-24 16:27:48.423 19806-20764/com.example.rini.listviewandroid W/System.err: at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
03-24 16:27:48.423 19806-20764/com.example.rini.listviewandroid W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:122)
03-24 16:27:48.423 19806-20764/com.example.rini.listviewandroid W/System.err: ... 17 more
グライド!これは、多くの機能を内蔵しており、メモリリーク、キャッシング、画像のダウンロードなど多くの苦痛を軽減します。あなたが特別な解決策を必要としない限り、私はそれを使用します。 –