android-asynctask – 带有asyncTask的Recyclerview适配器

android-asynctask – 带有asyncTask的Recyclerview适配器,第1张

概述我正在尝试使用RecyclerView的自定义适配器,但持有者是空的.我无法理解我错过了什么.请帮忙. 在MainActivity的AsyncTask中: @Override protected void onPostExecute(String result) { json_result = result; super.onPostExecute(resu 我正在尝试使用RecyclerVIEw的自定义适配器,但持有者是空的.我无法理解我错过了什么.请帮忙.

在MainActivity的AsyncTask中:

@OverrIDe    protected voID onPostExecute(String result) {        Json_result = result;        super.onPostExecute(result);        dialog.dismiss();        if (result == null) {            Toast.makeText(MainActivity.this,"error getting results...",Toast.LENGTH_LONG).show();        } else {            try {                JsONObject Json = new JsONObject(result);                Log.e(TAG,"create Json object");                JsONArray searchArray = Json.getJsONArray("Search");                Log.e(TAG,"Search");                for (int i = 0; i < searchArray.length(); i++) {                    Log.e(TAG,"run on length");                    JsONObject searchObject = searchArray.getJsONObject(i);                    Log.e(TAG,"create search object");                    String Title = searchObject.getString("Title");                    Log.e(TAG,"Title" + Title);                    String type = searchObject.getString("Type");                    Log.e(TAG,"Type" + type);                    String year = searchObject.getString("Year");                    Log.e(TAG,"Year" + year);                    String imdbID = searchObject.getString("imdbID");                    String poster = searchObject.getString("Poster");                    Log.e(TAG,"" + result);                    movIEList.add(new MovIE(Title,type,year,imdbID,poster));                    Log.e(TAG,"Add to adapter");                }            } catch (JsONException e) {                e.printstacktrace();                Toast.makeText(MainActivity.this,"error parsing results...",Toast.LENGTH_LONG).show();            }            adapter.notifyDataSetChanged();            Log.e(TAG,"Notify");        }    }

定制适配器:

public class Adapter extends    RecyclerVIEw.Adapter<Adapter.VIEwHolder> {private LruCache<String,Bitmap> bitmapCache;Context context;public static class VIEwHolder extends RecyclerVIEw.VIEwHolder {    public TextVIEw Title;    public TextVIEw year;    public TextVIEw type;    public ImageVIEw poster;    public VIEwHolder(VIEw itemVIEw) {        super(itemVIEw);        Title = (TextVIEw) itemVIEw.findVIEwByID(R.ID.txt_Title);        year = (TextVIEw) itemVIEw.findVIEwByID(R.ID.txt_year);        type = (TextVIEw) itemVIEw.findVIEwByID(R.ID.txt_rating);        poster = (ImageVIEw) itemVIEw.findVIEwByID(R.ID.imageVIEw);    }}private List<MovIE> mList;public Adapter(List<MovIE> mList) {    this.mList = mList;    int numImages = 4 * 1024 * 1024;    this.bitmapCache = new LruCache<String,Bitmap>(numImages) {        @OverrIDe        protected int sizeOf(String key,Bitmap value) {            // this is how to calculate a bitmap size in bytes.            // (bytes-in-a-row * height)            return value.getRowBytes() * value.getHeight();        }    };}@OverrIDepublic Adapter.VIEwHolder onCreateVIEwHolder        (VIEwGroup parent,int vIEwType) {    Context context = parent.getContext();    LayoutInflater inflater = LayoutInflater.from(context);    VIEw movIEVIEw = inflater.inflate(R.layout.card_vIEw,parent,false);    VIEwHolder vIEwHolder = new VIEwHolder(movIEVIEw);    return vIEwHolder;}@OverrIDepublic voID onBindVIEwHolder        (Adapter.VIEwHolder holder,int position) {    MovIE movIE = mList.get(position);    holder.Title.setText(movIE.getTitle());    holder.year.setText(movIE.getYear());    holder.type.setText(movIE.getType());    holder.poster.setVisibility(VIEw.INVISIBLE);    GetimageTask task = new GetimageTask(movIE,holder);    task.execute(movIE.getPoster_url());}@OverrIDepublic int getItemCount() {    return mList.size();}class GetimageTask extends AsyncTask<String,VoID,Bitmap> {    private final MovIE movIE;    private final VIEwHolder holder;    public GetimageTask(MovIE movIE,VIEwHolder holder) {        this.movIE = movIE;        this.holder = holder;    }    @OverrIDe    protected Bitmap doInBackground(String... params) {        //download:        String address = params[0];        Bitmap bitmap = httpHandler.getBitmap(address,null);        //save it in the cache for later:        if (bitmap != null) {            bitmapCache.put(address,bitmap);        }        return bitmap;    }    @OverrIDe    protected voID onPostExecute(Bitmap result) {        if (movIE.equals(holder.poster)) {            holder.poster.setVisibility(VIEw.VISIBLE);            holder.poster.setimageBitmap(result);        }    }}

}

我认为问题是我的电影类是空的,但我确实在asynctask中有JsON结果…

解决方法 您收到的数据,已保存在列表中 – > movIEList.
您是否在适配器中使用它并将适配器设置为recycler视图.

>在主类中初始化回收器视图
>初始化适配器并将movIEList作为arguement传递给创建的适配器对象.
>将适配器连接到recyclervIEw – > recyclerVIEw.setAdapter(适配器).

我认为这应该有效

总结

以上是内存溢出为你收集整理的android-asynctask – 带有asyncTask的Recyclerview适配器全部内容,希望文章能够帮你解决android-asynctask – 带有asyncTask的Recyclerview适配器所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1127925.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存