C
C
Chvalov2016-03-10 11:12:42
Java
Chvalov, 2016-03-10 11:12:42

How to pass int to another class in this case?

There is a Response.java class that takes an array of bytes from the controller.
In it, I parse the answer I need, and transfer the data to Activiti for display.

public class Response {

    private Response(byte[] data) {

        // Работа со статусом
        if (data.length==96){
            // Определяем состояния двигателя
            for (int i = 0; i < 8; i++) { //для первых 8-ми
                if(data[i+8] != 0){
                    getEngineDanger(i);
                    continue;
                }
                if(isSet(data[3], i)){
                    getEngineStarted(i); //если стартовала (тру)
                }else {
                    getEngineStopped(i);
                }
            }
        }
    }

    // Передаем состояние по двигателям
    public int getEngineDanger(int i){return i;}   // Двигатель в аварии
    public int getEngineStarted(int i){return i;}   // Двигатель в работе
    public int getEngineStopped(int i){return i;}   //Двигатель выключен

    // Побитовое смещение, определяем включен ли двигатель (Возращает boolean значение)
    static boolean isSet(byte value, int bit){
        return (value&(1<<bit))!=0;
    }

In MainActivity.java I have to get i with getEngineDanger, getEngineStarted and getEngineStopped
@Override
    public void onRead(Response response) {
        setEngineButtonStarted(response.getEngineStarted());
    }

But the trouble is that I don’t know how I can receive the necessary data, since I have to transfer the data to the Response with MainActivity (for example, in setEngineButtonStarted), he will return them to me.
How to be in this situation, how can I just receive data in the activity?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander_Fomin, 2016-03-10
@Alexander_Fomin

If I understand the essence of the question, then you can do this:

interface OnResponseCallback {
    void onResponse(int[] data);
}

class MainActivity implements OnResponseCallback {
    public MainActivity() {
        Response response = new Response(this);

    }

    @Override
    public void onResponse(int[] data) {
        //Обработка полученных данных
    }
}

class Response {
    OnResponseCallback mOnResponseComplete;
    public Response(OnResponseCallback onResponseComplete) {
        mOnResponseComplete = onResponseComplete;
        returnDataToMainActivity();
    }

    void returnDataToMainActivity() {
        mOnResponseComplete.onResponse(new int[] {1, 2, 3});
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question