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

时间:2020-03-16 12:25:06

标签: java android json android-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);

1 个答案:

答案 0 :(得分:0)

您必须从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);
        }
    }