我使用 listview 和自定义适配器。适配器加载图像存储和下载存储和显示。
问题,下载并安装所有文件,直到 imageview 出现随机照片。下载完成后,所有照片看起来都正常。
public class NewsListAdapter extends ArrayAdapter<NewsData>{
List<NewsData> items;
Context context;
public HaberListAdapter(Context context, int textViewResourceId,List<NewsData> haberArray) {
super(context, textViewResourceId, haberArray);
this.items = haberArray;
this.context=context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View v=convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.custom_haber_list, null);
holder = new ViewHolder();
holder.txtCaption = (TextView) v.findViewById(R.id.txtCaption);
holder.txtContent = (TextView) v.findViewById(R.id.txtContent);
holder.txtDate= (TextView) v.findViewById(R.id.txtDate);
holder.imageView= (ImageView) v.findViewById(R.id.image);
v.setTag(holder);
}else{
holder=(ViewHolder)v.getTag();
}
final NewsData o = items.get(position);
if (o != null) {
holder.txtCaption.setText(o.getCaption());
holder.txtContent.setText(o.getContent());
holder.txtDate.setText(o.getTarih());
if (o.getimageStatus()==0)
{
String[] params={ String.valueOf(o.getID()),o.getImageWebUrl()};
new setDownloadImage(holder.imageView,position).execute(params);
}
else
{
String imagePath = context.getFilesDir().toString() +"/news"+o.getID()+".jpg";
File fs=new File(imagePath);
if (fs.exists())
holder.imageView.setImageDrawable(Drawable.createFromPath(imagePath));
else
{
dataBase db=new dataBase(context);
db.setimageStatus(o.getID(), 0);
}
}
}
if (position % 2 == 0)
v.setBackgroundColor(Color.WHITE);
else
v.setBackgroundColor(Color.parseColor("#D4E4FD"));
return v;
}
class ViewHolder {
public TextView txtCaption,txtContent,txtDate;
public ImageView imageView;
}
class setDownloadImage extends AsyncTask<String,String,String>
{
private int position;
private final WeakReference<ImageView> imageViewReference;
public setDownloadImage(ImageView imageView,int position)
{
this.position=position;
imageViewReference = new WeakReference<ImageView>(imageView);
}
@Override
protected String doInBackground(String... params) {
String f_url=params[1];
String id=params[0];
int count;
try {
URL url = new URL(f_url);
URLConnection conection = url.openConnection();
conection.connect();
InputStream input = new BufferedInputStream(url.openStream(), 8192);
OutputStream output = new FileOutputStream(context.getFilesDir()+"/news"+id+".jpg");
byte data[] = new byte[1024];
while ((count = input.read(data)) != -1) {
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
return id;
} catch (Exception e) {
return "";
}
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (!result.equals(""))
{
ImageView imageView = imageViewReference.get();
String imagePath = context.getFilesDir().toString() +"/news"+result+".jpg";
imageView.setImageDrawable(Drawable.createFromPath(imagePath));
dataBase db=new dataBase(context);
db.setimageStatus(Integer.valueOf(result),1);
items.get(position).setimageStatus(1);
}
}
}
}