這裏是我的片段的代碼,我在asynctask中訪問一個web服務來獲取數據並在列表中顯示,我也懶加載圖像。當片段創建爲第一次輸出是正確的,它會在列表中顯示7個項目,問題是當片段在被銷燬後重新創建數據被重複時,即如果它第二次啓動,則列表顯示14個項目(早期7個項目7次再次被提取),每當它重新創建時,都會發生這種情況。列表中的項目比以前多了7個。儘管在On destroy中,我清除了適配器的數據,而adapter.getCount()顯示爲0,但仍然存在此問題。ListView每次創建片段時都會顯示重複的數據
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");
}
}
}
沒有工作我清除了列表並通知適配器..still同樣的問題。 – Ravi 2013-05-03 11:26:24
你有沒有通過調試或日誌檢查你的代碼的流程,檢查它在ArrayList中添加數據的位置,因爲我最近有類似的問題,這是一個很好的方法來解決它... – 2013-05-03 11:31:26
雅它工作,但我清除它在onPostExecute方法的結束。你可以解釋爲什麼發生這種情況,我的意思是carDetailList應該在onPostExecute方法之外自動銷燬 – Ravi 2013-05-03 11:46:56