Сообщество IT-специалистов
Ответы на любые вопросы об IT
Профессиональное развитие в IT
Удаленная работа для IT-специалистов
public class MainActivity extends Activity { public Elements title; public Elements content; public ArrayList<String> titleList = new ArrayList<>(); private ArrayAdapter<String> adapter; private ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView)findViewById(R.id.listView1); lv.setOnScrollListener(new InfiniteScrollListener(10) { @Override public void loadMore(int page, int totalItemsCount) { new NewThread(page).execute(); } }); //new NewThread().execute(); adapter = new ArrayAdapter<String>(this,R.layout.list_item, R.id.pro_item, titleList); lv.setAdapter(adapter); } public class NewThread extends AsyncTask<String, Void, String>{ int page = 0; NewThread(int page){ this.page=page; } @Override protected String doInBackground(String...arg){ Document doc; try{ titleList.clear(); for(int i = 1; i<=page;i++) { doc = Jsoup.connect(URL+"/?paged="+page).get(); title = doc.select(".genpost-entry-title "); content = doc.select(".genpost-entry-content"); //titleList.clear(); for (int j = 0; j<title.size(); j++) { titleList.add(title.get(j).text() + "\n\n" + content.get(j).text()); } } }catch (IOException e){ e.printStackTrace(); } return null; } @Override protected void onPostExecute(String result) /*List почему то не работает, говорит, что не переопределяет метод суперкласса*/ {adapter.clear();adapter.addAll(result);adapter.notifyDataSetChanged();} } public abstract class InfiniteScrollListener implements AbsListView.OnScrollListener { private int bufferItemCount = 10; private int currentPage = 0; private int itemCount = 0; private boolean isLoading = true; public InfiniteScrollListener(int bufferItemCount) { this.bufferItemCount = bufferItemCount; } public abstract void loadMore(int page, int totalItemsCount); @Override public void onScrollStateChanged(AbsListView view, int scrollState) { } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if (totalItemCount < itemCount) { this.itemCount = totalItemCount; if (totalItemCount == 0) { this.isLoading = true; } } if (isLoading && (totalItemCount > itemCount)) { isLoading = false; itemCount = totalItemCount; currentPage++; } if (!isLoading && (totalItemCount - visibleItemCount)<=(firstVisibleItem + bufferItemCount)) { loadMore(currentPage + 1, totalItemCount); isLoading = true; } } } }
И оно не работает