0

这是我的片段的代码,我在异步任务中访问 Web 服务以获取数据并显示在列表中,我也懒惰加载图像。当第一次创建片段时输出正确,它显示 7 个项目它应该的列表,问题是当片段在被破坏后重新创建时,数据会重复,即如果它第二次启动,列表显示14个项目(7个来自之前的项目,7个被再次获取)并且每次重新创建时都会发生这种情况, 没有。列表中的项目数比上一个多 7 个。虽然在 On destroy 我清除了适配器的数据并且 adapter.getCount() 显示为 0 但仍然存在此问题。

public class ServiceCarListFragment extends Fragment {

private String url;
private ArrayList<CarDetail> carDetailList = new ArrayList<CarDetail>();
private CarListAdapter adapter;
private ListView mList ;
private ProgressDialog progressDialog;

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    Log.d("Services", "On Create");
    url = getActivity().getIntent().getStringExtra("url");
    adapter = new CarListAdapter(getActivity() , carDetailList);
    new DownloadCarDetail().execute(url);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    Log.d("Services", "On CreateView");
    View v = inflater.inflate(R.layout.fragment_service_car_list,      container,false);
    mList = (ListView)v.findViewById(R.id.list);
    mList.setAdapter(adapter);      
    return v;
}

class DownloadCarDetail extends AsyncTask<String, String, ArrayList<CarDetail>>{



    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        progressDialog = ProgressDialog.show(getActivity(), null,   "Loading...",true);

    }

    @Override
    protected ArrayList<CarDetail> doInBackground(String... params) {
        // TODO Auto-generated method stub
        ArrayList<CarDetail> carDetailList = JsonParser.parseJson(params[0]);
        return carDetailList;
    }

    @Override
    protected void onPostExecute(ArrayList<CarDetail> carDetailList) {
        // TODO Auto-generated method stub
        //ServiceCarListFragment.this.carDetailList = carDetailList;
        //adapter = new CarListAdapter(getActivity(),ServiceCarListFragment.this.carDetailList);
        //mList.setAdapter(adapter);
        progressDialog.dismiss();
        ServiceCarListFragment.this.carDetailList.addAll(carDetailList);
        adapter.notifyDataSetChanged();
        for (CarDetail car : carDetailList) {
            // START LOADING IMAGES FOR EACH STUDENT
            car.loadImage(adapter);

    }

}

}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    carDetailList.clear();
    adapter.notifyDataSetChanged();
    Log.d("Services", String.valueOf(adapter.getCount()));
}

@Override
public void onDestroyView() {
    // TODO Auto-generated method stub
    super.onDestroyView();
    Log.d("Services", "On DestroyView");
}

@Override
public void onDetach() {
    // TODO Auto-generated method stub
    super.onDetach();
    Log.d("Services", "On Detach");
}

@Override
public void onAttach(Activity activity) {
    // TODO Auto-generated method stub
    super.onAttach(activity);
    Log.d("Services", "On Attach");
}

}

这是我正在使用的自定义适配器

 public class CarListAdapter extends BaseAdapter {

private ArrayList<CarDetail> items = new ArrayList<CarDetail>();
private Context context;


public CarListAdapter(Context context , ArrayList<CarDetail> items) {
    super();
    this.context = context;
    this.items = items;

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return items.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return items.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Log.d("Inside", "GetView");
    LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    ViewHolder holder = null;
    CarDetail car = items.get(position);

     if (convertView == null) {
            convertView = mInflater.inflate(R.layout.car_list_row, parent , false);
            holder = new ViewHolder();
            holder.tvCarName = (TextView) convertView.findViewById(R.id.tvCarName);
            holder.tvDailyPriceValue = (TextView) convertView.findViewById(R.id.tvWeeklyPriceValue);
            holder.tvWeeklyPriceValue = (TextView) convertView.findViewById(R.id.tvWeeklyPriceValue);
            holder.imgCar = (ImageView) convertView.findViewById(R.id.imgCar);
            convertView.setTag(holder);
    }
     else {
            holder = (ViewHolder) convertView.getTag();
        }

     holder.tvCarName.setText(car.getCarName());
        if (car.getImage() != null) {
            holder.imgCar.setImageBitmap(car.getImage());
        } else {
                // MY DEFAULT IMAGE
            holder.imgCar.setImageResource(R.drawable.ic_action_call);
        }

  return convertView;
 }

static class ViewHolder {

            TextView tvCarName;
            TextView tvDailyPriceValue;
            TextView tvWeeklyPriceValue;
            ImageView imgCar;
        }

}

这是模型类

public class CarDetail {

private String carId;
private String carName;
private String imageUrl;
private String thumbUrl;
private String dailyPrice;
private String weeklyPrice;
private String weekendPrice;
private String deposit;
private String minimumAge;
private String color;
private String make;
private String location;
private String bodyType;
private String fuelType;
private String transmission;
private String carType;
private String model;
private String description;
private Bitmap image;
private Bitmap thumbImage;
private CarListAdapter carAdapter;

public CarDetail() {
    super();
    // TODO Auto-generated constructor stub
}

public CarDetail(String carId, String carName, String imageUrl,
        String thumbUrl, String dailyPrice, String weeklyPrice,
        String weekendPrice, String deposit, String minimumAge,
        String color, String make, String location, String bodyType,
        String fuelType, String transmission, String carType, String model,
        String description) {
    super();
    this.carId = carId;
    this.carName = carName;
    this.imageUrl = imageUrl;
    this.thumbUrl = thumbUrl;
    this.dailyPrice = dailyPrice;
    this.weeklyPrice = weeklyPrice;
    this.weekendPrice = weekendPrice;
    this.deposit = deposit;
    this.minimumAge = minimumAge;
    this.color = color;
    this.make = make;
    this.location = location;
    this.bodyType = bodyType;
    this.fuelType = fuelType;
    this.transmission = transmission;
    this.carType = carType;
    this.model = model;
    this.description = description;

    // TO BE LOADED LATER - OR CAN SET TO A DEFAULT IMAGE
    this.image = null;
    this.thumbImage = null;
}

public String getCarId() {
    return carId;
}

public void setCarId(String carId) {
    this.carId = carId;
}

public String getCarName() {
    return carName;
}

public void setCarName(String carName) {
    this.carName = carName;
}

public String getImageUrl() {
    return imageUrl;
}

public void setImageUrl(String imageUrl) {
    this.imageUrl = imageUrl;
}

public String getThumbUrl() {
    return thumbUrl;
}

public void setThumbUrl(String thumbUrl) {
    this.thumbUrl = thumbUrl;
}

public String getDailyPrice() {
    return dailyPrice;
}

public void setDailyPrice(String dailyPrice) {
    this.dailyPrice = dailyPrice;
}

public String getWeeklyPrice() {
    return weeklyPrice;
}

public void setWeeklyPrice(String weeklyPrice) {
    this.weeklyPrice = weeklyPrice;
}

public String getWeekendPrice() {
    return weekendPrice;
}

public void setWeekendPrice(String weekendPrice) {
    this.weekendPrice = weekendPrice;
}

public String getDeposit() {
    return deposit;
}

public void setDeposit(String deposit) {
    this.deposit = deposit;
}

public String getMinimumAge() {
    return minimumAge;
}

public void setMinimumAge(String minimumAge) {
    this.minimumAge = minimumAge;
}

public String getColor() {
    return color;
}

public void setColor(String color) {
    this.color = color;
}

public String getMake() {
    return make;
}

public void setMake(String make) {
    this.make = make;
}

public String getLocation() {
    return location;
}

public void setLocation(String location) {
    this.location = location;
}

public String getBodyType() {
    return bodyType;
}

public void setBodyType(String bodyType) {
    this.bodyType = bodyType;
}

public String getFuelType() {
    return fuelType;
}

public void setFuelType(String fuelType) {
    this.fuelType = fuelType;
}

public String getTransmission() {
    return transmission;
}

public void setTransmission(String transmission) {
    this.transmission = transmission;
}

public String getCarType() {
    return carType;
}

public void setCarType(String carType) {
    this.carType = carType;
}

public String getModel() {
    return model;
}

public void setModel(String model) {
    this.model = model;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public Bitmap getImage() {
    return image;
}

public void setImage(Bitmap image) {
    this.image = image;
}

public Bitmap getThumbImage() {
    return thumbImage;
}

public void setThumbImage(Bitmap thumbImage) {
    this.thumbImage = thumbImage;
}

 public void loadImage(CarListAdapter carAdapter) {
        // HOLD A REFERENCE TO THE ADAPTER
        this.carAdapter = carAdapter;
        if (thumbUrl != null && !thumbUrl.equals("")) {
            new ImageLoadTask().execute(thumbUrl);
        }
 }

    // ASYNC TASK TO AVOID CHOKING UP UI THREAD
    private class ImageLoadTask extends AsyncTask<String, String, Bitmap> {

        @Override
        protected void onPreExecute() {
            Log.i("ImageLoadTask", "Loading image...");
        }

        // PARAM[0] IS IMG URL
        protected Bitmap doInBackground(String... param) {
            Log.i("ImageLoadTask", "Attempting to load image URL: " + param[0]);
            try {
                Bitmap b = JsonParser.downloadBitmap(param[0]);
                return b;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }

        protected void onProgressUpdate(String... progress) {
            // NO OP
        }

        protected void onPostExecute(Bitmap ret) {
            if (ret != null) {
                Log.i("ImageLoadTask", "Successfully loaded " + carName + " image");
                image = ret;
                if (carAdapter != null) {
                    // WHEN IMAGE IS LOADED NOTIFY THE ADAPTER
                    carAdapter.notifyDataSetChanged();
                }
            } else {
                Log.e("ImageLoadTask", "Failed to load " + carName + " image");
            }
        }
    }
4

2 回答 2

3

您需要在下一次调用异步方法之前清除 ArrayList 数据。在您的情况下,它将是carDetailLis.clear() new DownloadCarDetail().execute(url);或只是检查流程并清除它。

于 2013-05-03T11:16:51.470 回答
0

我建议你修改

ServiceCarListFragment.this.carDetailList.addAll(carDetailList);

ServiceCarListFragment.this.carDetailList = new ArrayList...
 carDetailList.addAll

并且您应该在多次调用服务器后验证 carDetailList 的长度是否相同。

于 2013-05-03T11:11:58.800 回答