0

これは、asynctask で Web サービスにアクセスしてデータを取得し、リストに表示するフラグメントのコードです。また、画像の読み込みが面倒です。フラグメントが初めて作成されたとき、出力は正しく、7 項目が表示されます。必要なリスト、問題は、データが破棄された後にフラグメントが再作成されるときです。つまり、2回目に開始された場合、リストには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

0

修正することをお勧めします

ServiceCarListFragment.this.carDetailList.addAll(carDetailList);

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

また、サーバーを数回呼び出した後、carDetailList の長さが同じであるかどうかを確認する必要があります。

于 2013-05-03T11:11:58.800 に答える