AsyncTask Arraylist.clear()不起作用

时间:2017-04-12 11:04:31

标签: java android arrays android-asynctask adapter

我无法清除数组中的数据。

首次运行时它有10个元素 第二次运行时它有+10个元素,总计20个。

我尝试了.clear()方法,notifyDataSetChanged(),但它们无法正常工作。

public void getCast() {
    new AsyncTask<Void, Void, Void>() {

        ArrayList<CastClass> castList = new ArrayList<>();

        @Override
        protected Void doInBackground(Void... voids) {

            if (castList != null) {
                castList.clear();
            }

            API api = new API();
            URL url = api.createCastUrl(getData.getMovieId());

            //RequestUrl
            try {
                String jsonResponse = NetworkUtils.getConnection(url);

                castList = CastJson.getMovieCastListFromJson(jsonResponse);

            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            mCastAdapter.setMoviesCastDataData(castList);
            mCastAdapter.notifyDataSetChanged();
            Log.d(TAG, "onPostExecute: CASTLIST INPUT" + castList.size());
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
04-12 16:27:58.626 1874-1960/vimalroxy.popularmoviesroxy D/API: Built URI Cast = http://api.themoviedb.org/3/movie/295693/credits?api_key=xxxxx
04-12 16:27:59.131 1874-1874/vimalroxy.popularmoviesroxy D/CastAdapter: setMoviesCastDataData: CAST LIST9
04-12 16:27:59.131 1874-1874/vimalroxy.popularmoviesroxy D/DetailActivity: onPostExecute: CASTLIST INPUT9
04-12 16:28:00.746 1874-1878/vimalroxy.popularmoviesroxy I/art: Do partial code cache collection, code=29KB, data=30KB
04-12 16:28:00.747 1874-1878/vimalroxy.popularmoviesroxy I/art: After code cache collection, code=29KB, data=30KB
04-12 16:28:00.747 1874-1878/vimalroxy.popularmoviesroxy I/art: Increasing code cache capacity to 128KB
04-12 16:28:05.879 1874-1959/vimalroxy.popularmoviesroxy D/API: Built URI Cast = http://api.themoviedb.org/3/movie/321612/credits?api_key=xxxxx
04-12 16:28:05.879 1874-1878/vimalroxy.popularmoviesroxy I/art: Do partial code cache collection, code=58KB, data=62KB
04-12 16:28:05.884 1874-1878/vimalroxy.popularmoviesroxy I/art: After code cache collection, code=57KB, data=62KB
04-12 16:28:05.884 1874-1878/vimalroxy.popularmoviesroxy I/art: Increasing code cache capacity to 256KB
04-12 16:28:06.455 1874-1874/vimalroxy.popularmoviesroxy D/CastAdapter: setMoviesCastDataData: CAST LIST19
04-12 16:28:06.455 1874-1874/vimalroxy.popularmoviesroxy D/DetailActivity: onPostExecute: CASTLIST INPUT19

ADAPTER:

public class CastAdapter extends RecyclerView.Adapter<CastAdapter.CastViewHolder> {

    private static final String TAG = "CastAdapter";

    private Context mContext;
    private ArrayList<CastClass> mMoviesCastData;

    public CastAdapter(Context context) {
        mContext = context;
    }

    @Override
    public CastViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.cast_item_view, null, false);
        return new CastViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final CastViewHolder holder, int position) {

        String name = mMoviesCastData.get(position).getCastName();

        String imageLink = mMoviesCastData.get(position).getCastImage();
        Uri uri = Uri.parse("https://image.tmdb.org/t/p/w150/" + imageLink);

        holder.cast_name.setText(name);

        Glide.with(mContext)
                .load(uri)
                .asBitmap()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
                .placeholder(R.drawable.person_icon)
                .into(new BitmapImageViewTarget(holder.cast_image) {
                    @Override
                    protected void setResource(Bitmap resource) {
                        RoundedBitmapDrawable circularBitmapDrawable =
                                RoundedBitmapDrawableFactory.create(mContext.getResources(), resource);
                        circularBitmapDrawable.setCircular(true);
                        holder.cast_image.setImageDrawable(circularBitmapDrawable);
                    }
                });
    }

    @Override
    public int getItemCount() {
        if (mMoviesCastData == null) return 0;
        return mMoviesCastData.size();
    }

    class CastViewHolder extends RecyclerView.ViewHolder {
        TextView cast_name;
        ImageView cast_image;

        public CastViewHolder(View itemView) {
            super(itemView);
            cast_name = (TextView) itemView.findViewById(R.id.cast_item_name);
            cast_image = (ImageView) itemView.findViewById(R.id.cast_item_picture);
        }
}

public void setMoviesCastDataData(ArrayList<CastClass> moviesCastData) {
    if (mMoviesCastData != null) {
        mMoviesCastData.clear();
        mMoviesCastData = moviesCastData;
    } else {
        mMoviesCastData = moviesCastData;
    }
    notifyDataSetChanged();
    Log.d(TAG, "setMoviesCastDataData: CAST LIST" + moviesCastData.size());
}

}

2 个答案:

答案 0 :(得分:1)

更改

public void setMoviesCastDataData(ArrayList<CastClass> moviesCastData) { 
  mMoviesCastData.clear(); 
  mMoviesCastData.addAll(moviesCastData); 
  notifyDataSetChanged(); 
  Log.d(TAG, "setMoviesCastDataData: CAST LIST" + moviesCastData.size()); 
}

更改构造函数

 public CastAdapter(Context context ,ArrayList<CastClass> mMoviesCastData) {
    mContext = context;
    this.mMoviesCastData = mMoviesCastData;
}

doInBackground()方法中将return null;更改为return castList;

答案 1 :(得分:0)

首先,我建议你在Asynctask中return castList

其次,在适配器中,不要重新分配Arraylist。

然后清除addAll

if (mMoviesCastData == null) {
    mMoviesCastData = moviesCastData; 
} else {
    mMoviesCastData.clear();
    mMoviesCastData.addAll(moviesCastData);
} 
notifyDataSetChanged();

如果您先初始化字段

,也可以删除空检查
private final Context mContext;
private final List<CastClass> mMoviesCastData;

public CastAdapter(Context context) {
    mContext = context;
    mMoviesCastData = new ArrayList<>() ;
}