59.- Mainactivity.java.txt

  • Uploaded by: Jason Anderson
  • 0
  • 0
  • December 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View 59.- Mainactivity.java.txt as PDF for free.

More details

  • Words: 127
  • Pages: 2
package com.danieldisu.rxjavaintro; import import import import import import import import

android.os.Bundle; android.support.v4.app.ListFragment; android.support.v7.app.AppCompatActivity; android.util.Log; android.view.Menu; android.view.MenuItem; android.widget.ArrayAdapter; android.widget.ListView;

import com.google.common.collect.Lists; import com.jakewharton.rxbinding.widget.RxAdapterView; import java.util.List; import rx.Observable; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; public class MainActivity extends AppCompatActivity { private static String TAG = "RX_JAVA_INTRO"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListFragment listFragment = (ListFragment) getSupportFragmentManager().findFragmentById(R.id.listFragment); RestClient.HNService hnService = RestClient.getHNService(); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1); listFragment.setListAdapter(adapter); hnService.getNewStories() .subscribeOn(Schedulers.io()) .map(newStories -> getItemObservables(hnService, newStories)) .flatMap(Observable::merge) .doOnNext(hnItem1 -> Log.d(TAG, "item received " + hnItem1.id)) .take(10) .observeOn(AndroidSchedulers.mainThread()) .subscribe((hnItem) -> addItemToList(hnItem, adapter), Throwable::printStackTrace); ListView listView = listFragment.getListView(); RxAdapterView.itemClicks(listView) .subscribe(position -> Log.d(TAG, "Item at position " + position + " pressed")); } private void addItemToList(RestClient.HNItem hnItem, ArrayAdapter<String> adapter) { adapter.add(hnItem.title); }

private List> getItemObservables(RestClient.HNService hnService, RestClient.NewStories newStories) { return Lists.transform(newStories, hnService::getItem); } }

Related Documents

59
November 2019 56
59
June 2020 35
59
October 2019 83
59
July 2020 34
59
November 2019 50
59
December 2019 40

More Documents from "Silviu"

70.- Constraint Layout.pptx
December 2019 5
Cat.txt
December 2019 1
59.- Mainactivity.java.txt
December 2019 3
Libro1
May 2020 18
December 2019 33