首页 > 解决方案 > 为什么我的数组中没有任何元素

问题描述

所以我创建了一个数组来存储来自 jsoup 调用的数据,当我尝试运行这个方法来实际放入动态数据时,它根本没有放入任何东西。真是怪怪的

public class MainActivity extends AppCompatActivity {

    List<Book> lstBook;

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

        lstBook = new ArrayList<>();
        getWebsite();
        lstBook.add(new Book("Kenichi: The Mightiest Disciple", "All you really got to know is that this is THE Greatest Book Ever!", "http://fmcdn.Bookhere.com/store/Book/18607/cover.jpg?token=7ef7d63df101d2e3ff52c9c8e15aa6ae4abc176e&amp;ttl=1594666800&amp;v=1594289853"));
        lstBook.add(new Book("Kenichi: The Mightiest Disciple", "All you really got to know is that this is THE Greatest Book Ever!", "http://fmcdn.Bookhere.com/store/Book/18607/cover.jpg?token=7ef7d63df101d2e3ff52c9c8e15aa6ae4abc176e&amp;ttl=1594666800&amp;v=1594289853"));
        lstBook.add(new Book("Kenichi: The Mightiest Disciple", "All you really got to know is that this is THE Greatest Book Ever!", "http://fmcdn.Bookhere.com/store/Book/18607/cover.jpg?token=7ef7d63df101d2e3ff52c9c8e15aa6ae4abc176e&amp;ttl=1594666800&amp;v=1594289853"));
        lstBook.add(new Book("Kenichi: The Mightiest Disciple", "All you really got to know is that this is THE Greatest Book Ever!", "http://fmcdn.Bookhere.com/store/Book/18607/cover.jpg?token=7ef7d63df101d2e3ff52c9c8e15aa6ae4abc176e&amp;ttl=1594666800&amp;v=1594289853"));
        lstBook.add(new Book("Kenichi: The Mightiest Disciple", "All you really got to know is that this is THE Greatest Book Ever!", "http://fmcdn.Bookhere.com/store/Book/18607/cover.jpg?token=7ef7d63df101d2e3ff52c9c8e15aa6ae4abc176e&amp;ttl=1594666800&amp;v=1594289853"));

这些静态输入填充应用程序屏幕。

        RecyclerView myrv = (RecyclerView) findViewById(R.id.recyclerview_id);
        RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(this, lstBook);
        myrv.setLayoutManager(new GridLayoutManager(this, 3));
        myrv.setAdapter(myAdapter);

    }

    private void getWebsite() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                final StringBuilder builder = new StringBuilder();
                try {
                    Document doc = Jsoup.connect("https://www.Bookhere.cc/ranking/").get();
                    Elements description = doc.select("p.Book-list-1-item-title");
                    int length = description.size();
                    String cont = description.eq(1).text();
                    for (int i = 0; i < 10; i++) {
                        String imgUrl = description.eq(i).text();
                        String title = doc.select("img.Book-list-1-cover").eq(i).attr("src");
                        Book test = (new Book(title,"All you really got to know is that this is THE Greatest Book Ever!","http://fmcdn.Bookhere.com/store/Book/18607/cover.jpg?token=7ef7d63df101d2e3ff52c9c8e15aa6ae4abc176e&amp;ttl=1594666800&amp;v=1594289853"));
                        lstBook.add(test);
                        Log.d("Yuh", lstManga.toString());

这个输出[com.example.mangadrip.Manga@f128bbf, com.example.mangadrip.Manga@633e28c]是我认为问题的根源。


                        Log.d("Yuh", test.getTitle());
                    }
                } catch (IOException ignored) {
                    Log.d("Yuh","This duo");
                }
            }
        }).start();
    }
}

所以当我编译这个应用程序时,我没有得到任何输入,就好像没有任何东西被放进去一样lstBook。它唯一保留的是方法调用下的手动输入。

解决方案

runOnUiThread(new Runnable() { public void run() { myAdapter.notifyDataSetChanged(); }});

标签: javaandroid-studiojsoup

解决方案


我猜你有一个多线程问题。您从不同的线程访问列表,没有同步,所以期待任何类型的问题。您可以使用线程安全列表,但老实说,我会考虑重新设计多线程。最好的解决方案是,仅从一个线程访问列表。


推荐阅读