Guest User

Untitled

a guest
Feb 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2.  
  3.  
  4. public static final String TAG = "mLog";
  5.  
  6. @BindView(R.id.recycler_view)
  7. RecyclerView recyclerView;
  8.  
  9. @BindView(R.id.imageView)
  10. ImageView imageView;
  11.  
  12. private RecyclerView.Adapter adapter;
  13. private List<ListItem> listItems;
  14. ListItem item;
  15. 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";
  16. Document document;
  17. String head = "something";
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main);
  22. ButterKnife.bind(this);
  23.  
  24. JsoupAsyncTask jsoupAsyncTask = new JsoupAsyncTask();
  25. jsoupAsyncTask.execute();
  26.  
  27.  
  28. recyclerView.setLayoutManager(new LinearLayoutManager(this));
  29.  
  30.  
  31. listItems = new ArrayList<>();
  32.  
  33.  
  34. item = new ListItem(
  35. head,
  36. "desc1"
  37. );
  38. listItems.add(item);
  39.  
  40. adapter = new MyRecViewAdapter(listItems, this);
  41. recyclerView.setAdapter(adapter);
  42.  
  43.  
  44. }
  45.  
  46. private class JsoupAsyncTask extends AsyncTask<Void, Void, Void> {
  47.  
  48.  
  49. @Override
  50. protected void onPreExecute() {
  51. super.onPreExecute();
  52. }
  53.  
  54.  
  55. @Override
  56. protected Void doInBackground(Void... voids) {
  57.  
  58. try {
  59. document = Jsoup.connect(URL).get();
  60. head = document.title();
  61. } catch (IOException e ) {
  62. e.printStackTrace();
  63. }
  64.  
  65. return null;
  66. }
  67.  
  68. @Override
  69. protected void onPostExecute(Void aVoid) {
  70. }
  71. }
  72.  
  73. }
  74.  
  75. public class MyRecViewAdapter extends RecyclerView.Adapter<MyRecViewAdapter.ViewHolder>{
  76.  
  77. private List<ListItem> listItems;
  78. private Context context;
  79.  
  80. public MyRecViewAdapter(List<ListItem> listItems, Context context) {
  81. this.listItems = listItems;
  82. this.context = context;
  83. }
  84.  
  85. @Override
  86. public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  87. View v = LayoutInflater.from(parent.getContext())
  88. .inflate(R.layout.list_item, parent, false);
  89. //return our view
  90. return new ViewHolder(v);
  91. }
  92.  
  93. @Override
  94. public void onBindViewHolder(ViewHolder holder, int position) {
  95. ListItem listItem = listItems.get(position);
  96.  
  97. holder.tvHead.setText(listItem.getHead());
  98. holder.tvDesc.setText(listItem.getDesc());
  99. }
  100.  
  101. @Override
  102. public int getItemCount() {
  103. return listItems.size();
  104. }
  105.  
  106. public static class ViewHolder extends RecyclerView.ViewHolder {
  107.  
  108. public TextView tvHead;
  109. public TextView tvDesc;
  110.  
  111. public ViewHolder(View itemView) {
  112. super(itemView);
  113.  
  114. tvHead = (TextView) itemView.findViewById(R.id.tv_head);
  115. tvDesc = (TextView) itemView.findViewById(R.id.tv_desc);
  116. }
  117. }
  118. }
  119.  
  120. public class ListItem {
  121.  
  122. private String head;
  123. private String desc;
  124.  
  125. public ListItem(String head, String desc) {
  126. this.head = head;
  127. this.desc = desc;
  128. }
  129.  
  130.  
  131. public String getHead() {
  132. return head;
  133. }
  134.  
  135. public String getDesc() {
  136. return desc;
  137. }
  138.  
  139.  
  140. }
Add Comment
Please, Sign In to add comment