I use Android glide to upload deleted images, but here I met a strange problem, the image of which is not displayed on the first screen, but while I scroll down and scroll up, these images become normally displayed.
I also looked for such a question and found duplicate , and after trying such actions, it still does not work, and you may need to check the code, here it is:
public class SiteAdapter extends ArrayAdapter<Site> {
private int resourceId;
private List<Site> sites = null;
private Context context;
private SiteHolder siteHolder;
public SiteAdapter(Context context, int resource, List<Site> objects) {
super(context, resource, objects);
this.context = context;
this.resourceId = resource;
this.sites = objects;
}
@Override
public Site getItem(int position) {
return sites.get(position);
}
@Override
public int getCount() {
return sites.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Site site = getItem(position);
View view = convertView;
siteHolder = new SiteHolder();
if (view == null) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(resourceId, null);
}
siteHolder.image = (ImageView) view.findViewById(R.id.thumbnail);
siteHolder.address = (TextView)view.findViewById(R.id.address);
siteHolder.mallName = (TextView) view.findViewById(R.id.name);
siteHolder.distance = (TextView) view.findViewById(R.id.distance);
siteHolder.address.setText(site.getAddress());
siteHolder.mallName.setText(site.getName());
siteHolder.distance.setText("<" + site.getDistance() + "m");
ImageTask task = new ImageTask();
task.execute("http://xxxx/springmvc/getFirst/" + site.getName());
return view;
}
public String getImgUrl(String str){
String data = "";
try {
URL u = new URL(str);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
InputStream is = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String content = "";
StringBuffer sb = new StringBuffer();
while((content = br.readLine()) != null){
sb.append(content);
}
data = sb.toString();
br.close();
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
class ImageTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... str) {
String data = "";
data = getImgUrl(str[0]);
return data;
}
@Override
protected void onPostExecute(String s) {
Glide.with(context)
.load(s)
.fitCenter()
.into(siteHolder.image);
}
}
class SiteHolder{
ImageView image;
TextView mallName;
TextView address;
TextView distance;
}
}
if anyone who has met such a scene before, thanks in advance.
source
share