简体   繁体   中英

Pass live data in another activity

I'm making an android application where in my first activity i read some sensor values and need to put them in the second activity in a GraphView linegraph.everything works fine if i have the graph in my first activity The code for my first activity is:

if (crc == crcTel) {


                            String[] s = rxData.split(" ");
                            if (s.length > 3) {

                                if( tryParseDouble( s[0] )) {
                                    temp = Double.parseDouble( s[0] );
                                }

                                if( tryParseDouble( s[1] )) {
                                    flow = Double.parseDouble( s[1] );
                                }

                                if( tryParseDouble( s[2] )) {
                                    press = Double.parseDouble( s[2] );
                                }

                                if(cntLines == 0)
                                {
                                    Tmin = Tmax = temp;
                                    Fmin = Fmax = flow;
                                    Pmin = Pmax = press;
                                }

                                if(Tmin > temp )
                                    Tmin = temp;
                                if(Tmax < temp )
                                    Tmax = temp;


                                if(Fmin > flow )
                                    Fmin = flow;
                                if(Fmax < flow )
                                    Fmax = flow;


                                if(Pmin > press )
                                    Pmin = press;
                                if(Pmax < press )
                                    Pmax = press;


                                cas = new ChartAxisScale( Fmin, Fmax);

                                viewport.setMinY(cas.min_value);
                                viewport.setMaxY(cas.max_value);

                                cntLines++;


                                cas = new ChartAxisScale( Tmin, Tmax);
                                secondScale1.setMinY( cas.min_value );
                                secondScale1.setMaxY( cas.max_value );


                                textViewTemp.setText("Temperature: " + s[0] + " [°C]");
                                textViewFlow.setText("Flow: " + s[1] + " [l/h]");
                                textViewPress.setText("Pressure: " + s[2] + " [mBar]");

can anyone help me with any advice about passing the read values to the second activity graph? In the second activity i only mentioned the default graph values with the axis and datapoints with series 1 and 2 for the flow and temperature.

GraphView graphView = (GraphView) findViewById(R.id.graph2);

    series = new LineGraphSeries<DataPoint>();
    graphView.addSeries(series);

    series2 = new LineGraphSeries<DataPoint>();
    graphView.addSeries(series2);
    series2.setColor( Color.RED );



    viewport = graphView.getViewport();
    viewport.setYAxisBoundsManual(true);
    viewport.setMinY(0);
    viewport.setMaxY(200);
    viewport.setScrollable(true);
    viewport.setScalable( true );
    viewport.isScrollable();
    viewport.scrollToEnd();

    secondScale1 = graphView.getSecondScale();

    graphView.getSecondScale().addSeries(series2);
    graphView.getSecondScale().setMinY(0);
    graphView.getSecondScale().setMaxY(100);

Here is a rough example of how you could use repositories in order to decouple the retrieval and management (persisting, mapping, etc.) of data away from your view. By doing this you can use these values anywhere in your app and don't have to worry about passing values between Activities/Fragments. Also, I saw your sensor data variable is named rxData and so assumed you are wanting this app to be reactive.

Solution

Observe temperatureDataPoints and flowDataPoints in your graph activity. The classes below are in the order they are called in the rx stream.

Note: you can start the observation of values in another activity. Also ensure that there is only one instance of each of your repositories and sensor.

GraphViewModel

class GraphViewModel constructor(
    private val temperatureInteractor: TemperatureInteractor,
    private val flowInteractor: FlowInteractor,
    private val schedulers: AppSchedulers
) : ViewModel() {

    private val disposables = CompositeDisposable()

    val flowDataPoints: LiveData<List<Pair<Double, Double>>> get() = _flowDataPoints
    private val _flowDataPoints = MutableLiveData<List<Pair<Double, Double>>>()

    val temperatureDataPoints: LiveData<List<Pair<Double, Double>>> get() = _temperatureDataPoints
    private val _temperatureDataPoints = MutableLiveData<List<Pair<Double, Double>>>()

    init {
        observeTemperatureValues()
        observeFlowValues()        
    }

    private fun observeTemperatureValues() {
        temperatureInteractor
            .getTemperatureDataPoints()
            .subscribeOn(appSchedulers.ioScheduler)
            .observeOn(appSchedulers.mainThread())
            .subscribeBy(
                    onNext = { _temperatureDataPoints.postValue(it) }
            )
            .addTo(disposables)

    }

    private fun observeFlowValues() {
        flowInteractor
            .getFlowDataPoints()
            .subscribeOn(appSchedulers.ioScheduler())
            .observeOn(appSchedulers.mainThread())
            .subscribeBy(
                    onNext = { _flowDataPoints.postValue(it) }
            )
            .addTo(disposables)
    }

    override fun onCleared() {
        disposables.clear()
        super.onCleared()
    }

}

TemperatureInteractor

class TemperatureInteractor constructor(
    private val temperatureRepository: TemperatureRepository
) {

    fun getTemperatureDataPoints(): Observable<List<Pair<Double, Double>>> {
        // Any business logic can sit here
        return temperatureRepository.observeTemperature()
    }

}

TemperatureRepository

@Singleton
class TemperatureRepository constructor(
    private val sensor: Sensor,
    private val dataStore: DataStore
) {

    fun observeTemperature(): Observable<List<Pair<Double, Double>>> {
        // sensor.observeTemperature()
        // Do some mapping here e.g. mapping temperature values against time
        // Save new temperature
        // Emit new values
        return Observable.just(listOf())
    }

}

Sensor

@Singleton
class Sensor {

    fun observeTemperature(): Observable<Double> {
        // Start observing temperature, if not already
        return Observable.just(10.00)
    }

}

interface DataStore {
    fun updateTemperatureHistory(temperatures: List<Pair<Double, Double>>)
    fun getTemperatureHistory(): List<Pair<Double, Double>>
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM