开发者社区> 问答> 正文

如何使用AsyncTask从JSON获取数据并将其放入RecyclerView中?

我正在使用RecyclerView显示动态数据,目前,我正在从strings.xml文件中获取数据,但是我想从JSON中获取数据并使用AsyncTask在recyclerview中对其进行更新。谁能告诉我该怎么做?请帮我。

这是我的RecyclerAdapter.class

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
    ArrayList<NewModel>newModels;
    Context context;
    public RecyclerAdapter(ArrayList<NewModel>newModels, Context context){
        this.newModels = newModels;
        this.context = context;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recy1_layout, parent,false);
        return new ViewHolder(v);
    }
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.imageView.setImageResource(newModels.get(position).getSociallogo());
        holder.textView.setText(newModels.get(position).getSocailtext());
    }
    @Override
    public int getItemCount() {
        return newModels.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textView;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView =itemView.findViewById(R.id.thumbimg);
            textView = itemView.findViewById(R.id.thumb_desc);
        }
    }
}

数据设置程序NewModel.class:

public class NewModel implements Parcelable {
    int thumbimage;
    String thumbtext;
    public NewModel(int thumbimage,String thumbtext){
        this.thumbimage= thumbimage;
        this.thumbtext = thumbtext;


    }
    public int getSociallogo(){
        return thumbimage;
    }
    public String getSocailtext(){
        return thumbtext;
    }

    public NewModel(Parcel ins) {
        // put your data using = in.readString();
        this.thumbtext= ins.readString();
        this.thumbimage = ins.readInt();

    }
    public static final Creator<NewModel> CREATOR = new Creator<NewModel>() {
        @Override
        public NewModel createFromParcel(Parcel ins) {
            return new NewModel(ins);
        }

        @Override
        public NewModel[] newArray(int size) {
            return new NewModel[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(thumbtext);
        parcel.writeInt(thumbimage);

    }
}

MainActivity.java

TypedArray thumbimages =getResources().obtainTypedArray(R.array.thumnail_images);
    String[] thumbdesc = getResources().getStringArray(R.array.thumbnail_desc);
    newModels = new ArrayList<>();
    for (int i =0; i<thumbimages.length(); i++){
        NewModel newModel =new NewModel(thumbimages.getResourceId(i, 0), thumbdesc[i]);
        newModels.add(newModel);
    }
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL,false);
    readapter= new RecyclerAdapter(newModels,getContext());
    recyclerView1.setLayoutManager(linearLayoutManager);
    recyclerView1.setItemAnimator(new DefaultItemAnimator());
    recyclerView1.setAdapter(readapter);

问题来源:Stack Overflow

展开
收起
montos 2020-03-22 15:47:24 900 0
1 条回答
写回答
取消 提交回答
  • 您必须从api获取数据,将其解析为ArrayList,然后使用notifyDataSetChanged进行更新。

    private void fetchData() {
                AsyncTask<Integer, Void, Void> task = new AsyncTask<Integer, Void, Void>() {
                    @Override
                    protected Void doInBackground(Integer... integers) {
                        OkHttpClient client = new OkHttpClient();
                        Request request = new Request.Builder().url("http://url.com/api/data").build();
                        try {
    
                            //Parse data to newModels
    
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (JSONException e) {
                            System.out.print("End of  content");
                        }
                        return null;
                    }
    
                    @Override
                    protected void onPostExecute(Void aVoid) {
                        // notify adapter
                        readapter.notifyDataSetChanged();
                    }
                };
    
                task.execute(id);
            }
        }
    

    回答来源:Stack Overflow

    2020-03-22 15:47:58
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载

相关实验场景

更多