java - 为什么我的数组中没有任何元素
问题描述
所以我创建了一个数组来存储来自 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&ttl=1594666800&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&ttl=1594666800&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&ttl=1594666800&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&ttl=1594666800&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&ttl=1594666800&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&ttl=1594666800&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(); }});
解决方案
我猜你有一个多线程问题。您从不同的线程访问列表,没有同步,所以期待任何类型的问题。您可以使用线程安全列表,但老实说,我会考虑重新设计多线程。最好的解决方案是,仅从一个线程访问列表。
推荐阅读
- react-native - 将 expo 应用程序转换为 react-native 后无法运行 ReactNative 应用程序
- typescript - 带有dayjs相对时间的打字稿警告
- javascript - 未捕获的类型错误:无法读取属性
- node.js - 使用 webpack 5 在电子上预加载脚本
- amazon-web-services - 为什么当我尝试终止 EC2 新实例时会创建它?
- amazon-web-services - 使用 PartiQL (DynamoDB) 在列表中选择某个地图元素
- python - 禁止登录 IRC 频道,以 twitch 为例
- django - 在 Heroku (Django) 上部署后如何恢复数据
- ruby-on-rails - Rails 6 Active Storage - 当您登陆带有图像的页面时,我的用户头像和帖子图像似乎在闪烁(因为它们正在加载两次)
- typescript - 在 TypeScript 中创建通用代理函数