如何在AsyncTask中将Jsoup数据设置为RecyclerView

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在AsyncTask中将Jsoup数据设置为RecyclerView相关的知识,希望对你有一定的参考价值。

我正在从URL解析网站标题,并将此标题设置为RecyclerView项目。当我调用构造函数时,但我必须使用AsyncTasck来正确地工作Jsoup。如何将收到的数据设置为RecyclerView

我可以做公共MyAsyncTasck而不是私人,但我现在不是,如果它是正确的。

我的MainActivity.java

public class MainActivity extends AppCompatActivity {

    public static final String TAG  = "mLog";

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    @BindView(R.id.imageView)
    ImageView imageView;

    private RecyclerView.Adapter adapter;
    private List<ListItem> listItems;
    ListItem item;

    String URL = "https://sinoptik.ua/%D0%BF%D0%BE%D0%B3%D0%BE%D0%B4%D0%B0-
    %D0%B2%D0%B8%D0%BD%D0%BD%D0%B8%D1%86%D0%B0";

    Document document;

    // Received data from Jsoup
    String head = "something";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        MyAsyncTask myAsyncTask = new MyAsyncTask();
        myAsyncTask.execute();

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        listItems = new ArrayList<>();

        item = new ListItem(head, "desc1");
        listItems.add(item);

        adapter = new MyRecViewAdapter(listItems, this);
        recyclerView.setAdapter(adapter);
    }

    private class MyAsyncTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

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

            try {
                document = Jsoup.connect(URL).get();
                head = document.title();
            } catch (IOException e ) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
          // ?????? 
        }
    }
}

MyRecViewAdapter.java

public class MyRecViewAdapter extends RecyclerView.Adapter<MyRecViewAdapter.ViewHolder>{

    private List<ListItem> listItems;
    private Context context;

    public MyRecViewAdapter(List<ListItem> listItems, Context context) {
        this.listItems = listItems;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item, parent, false);
        //return our view
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ListItem listItem = listItems.get(position);

        holder.tvHead.setText(listItem.getHead());
        holder.tvDesc.setText(listItem.getDesc());
    }

    @Override
    public int getItemCount() {
        return listItems.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView tvHead;
        public TextView tvDesc;

        public ViewHolder(View itemView) {
            super(itemView);

            tvHead = (TextView) itemView.findViewById(R.id.tv_head);
            tvDesc = (TextView) itemView.findViewById(R.id.tv_desc);
        }
    }
}

ListItem.java

public class ListItem {

    private String head;
    private String desc;

    public ListItem(String head, String desc) {
        this.head = head;
        this.desc = desc;
    }


    public String getHead() {
        return head;
    }

    public String getDesc() {
        return desc;
    }
}
答案

您需要使用AsyncTask将从API调用中获取的数据设置到listItems中的RecyclerView中。然后你需要调用notifyDataSetChanged以显示你的RecyclerView中最新更新的内容。

因此,您需要修改doInBackground中的AsyncTask函数,如下所示。

private class MyAsyncTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

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

        try {
            document = Jsoup.connect(URL).get();
            listItems.clear();
            ListItem item = new ListItem(document.title, document.desc);
            listItems.add(item);
            adapter.notifyDataSetChanged();

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

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
      // ?????? 
    }
}

以上是关于如何在AsyncTask中将Jsoup数据设置为RecyclerView的主要内容,如果未能解决你的问题,请参考以下文章

在 AsyncTask 完成后做某事

使用AsyncTask在android中创建JSoup类

在AsyncTask中更新UI

使用 JSoup 帮助抓取 HTML

如何在 android 中将 asynctask 代码更改为 rxandroid 代码?

在 Asynctask 中将视图作为参数发送