我的应用程序是红色的,但只有listview有一个小问题,我向下滚动图像更改,是不对的。我认为问题在于功能。首先我设置一个标志,然后我加载数据。
my adapter:
public class BinderData_bioscoop extends BaseAdapter {
LayoutInflater inflater;
List<HashMap<String,String>> BioscopenLijst;
ViewHolder holder;
public BinderData_bioscoop() {}
public BinderData_bioscoop(Activity act, List<HashMap<String,String>> map) {
this.BioscopenLijst = map;
inflater = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return BioscopenLijst.size();
}
public Object getItem(int arg0) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null){
vi = inflater.inflate(R.layout.list_row_bioscoop, null);
holder = new ViewHolder();
holder.naam = (TextView)vi.findViewById(R.id.naam); // naam
holder.poster =(ImageView)vi.findViewById(R.id.poster); // logo
vi.setTag(holder);
} else{
holder = (ViewHolder)vi.getTag();
}
// Invoegen van variabels
holder.naam.setText(BioscopenLijst.get(position).get("naam")); // naam
holder.poster.setTag(BioscopenLijst.get(position).get("logo")); //logo
new GetPoster().execute(holder.poster);
return vi;
}
static class ViewHolder{
TextView naam;
ImageView poster;
}
}
班级“getposter”
public class GetPoster extends AsyncTask<ImageView, Void, Bitmap> {
ImageView imageView = null; //imagevieuw waar de foto moet komen
Bitmap return_var_DIB;
@Override
protected Bitmap doInBackground(ImageView... imageViews) { //doen in de achtergrond
this.imageView = imageViews[0];
BitmapFactory.Options options;
String poster_key = imageViews[0].getTag().toString();
if(poster_key.equals("")) {
/*=== niet's doen vanwege geen key ===*/
return_var_DIB = null;
} else {
/* === uitvoeren van code wanneer er een key is ===*/
poster_key = poster_key + ".jpg"; //exstentie aan de key toevoegen
//app map zoeken
File sdCard = Environment.getExternalStorageDirectory(); // Ophalen van de SD kaart locatie
/*
File android_directory = new File(sdCard.getAbsolutePath() + "/android"); //Aanmaken van de map
android_directory.mkdirs(); // map aanmaken
File android_data_directory = new File(sdCard.getAbsolutePath() + "/android/data"); //Aanmaken van de map
android_data_directory.mkdirs(); // map aanmaken
File mc_directory = new File(sdCard.getAbsolutePath() + "/android/data/be.me.app"); //Aanmaken van de map
mc_directory.mkdirs(); // map aanmaken
*/
File directory = new File(sdCard.getAbsolutePath() + "/android/data/be.app.me/case"); //Aanmaken van de map
directory.mkdirs(); // map aanmaken
String poster_pad = sdCard + "/android/data/be.me.app/case/" + poster_key;
File poster = new File(directory, poster_key); // aanmaken
if (poster.exists()) {
//output poster
return_var_DIB = BitmapFactory.decodeFile(poster_pad); //file to bitmap
} else {
//Downloaden
return_var_DIB = download_Image((String) poster_key, (File) poster, (String) poster_pad);
}
}
return return_var_DIB;
}
@Override
protected void onPostExecute(Bitmap result) { //als alles klaar is
if(result != null) {
imageView.setImageBitmap(result);
imageView.setTag("");
}
}
private Bitmap download_Image(String key, File poster, String poster_pad) { //foto downloaden
Bitmap poster_bmp = null;
//
try {
URL url = new URL("http://www.app.de/data/foto_adapter.php?key=" + key);
URLConnection connection = url.openConnection();
connection.connect();
InputStream input = new BufferedInputStream(url.openStream()); // het bestand downloaden
OutputStream output = new FileOutputStream(poster); // opslaan van het bestand
OutputStreamWriter osw = new OutputStreamWriter(output); //van het bestand
byte data[] = new byte[1024];
//total = 0;
int count;
while ((count = input.read(data)) != -1) {
//total += count;
// Publish the progress
//publishProgress((int) (total * 100 / fileLength)); // updaten progress bar
output.write(data, 0, count);
}
poster_bmp = BitmapFactory.decodeFile(poster_pad);
osw.flush(); // legen van de output
osw.close(); // sluiten an de output
input.close(); // (lege) input sluiten
} catch (Exception e) {
// Error Log
Log.e("Error", e.getMessage());
e.printStackTrace();
}
//
return poster_bmp;
}
}