无法在函数接口内将参数作为arraylist传递?

时间:2019-07-16 14:33:17

标签: java android retrofit2

示例是mvp 与retroit2 GetNoticeIntractorImpl类内部的问题 所以我想在函数内部传递一个这样的函数。 response.body()。getNoticeArrayList() 但是每次我尝试 该功能显示为红色且未知 我不知道为什么。 为什么不带功能:getNoticeArrayList() 我该怎么做才能通过此功能而不会出现错误?

image // MainActivity类

public class MainActivity extends AppCompatActivity {

    private final String TAG = MainActivity.class.getSimpleName();

    private RecyclerView recyclerView;
    private MainContract.presenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        presenter = new MainPresenterImpl(this,new GetNoticeIntractorImpl());
        presenter.requestDataFromServer();

    }
}



// GetNoticeIntractorImpl class

public class GetNoticeIntractorImpl implements MainContract.GetNoticeIntractor {
    List<ApiObject> list = new ArrayList<>();
    @Override
    public void getNoticeArrayList(final OnFinishedListener onFinishedListener) {
        ApiUtil.getServiceClass().getAllPost().enqueue(new Callback<List<ApiObject>>() {
            @Override
            public void onResponse(Call<List<ApiObject>> call, Response<List<ApiObject>> response) {
                if(response.isSuccessful()){
                  List<ApiObject> postList = response.body();
                   onFinishedListener.onFinished(response.body().getNoticeArrayList());
                  //Log.i(TAG, "R_count00 " + postList.size());
                }
            }
            @Override
            public void onFailure(Call<List<ApiObject>> call, Throwable t) {
                //showErrorMessage();
                Log.d(TAG, "error loading from API");
            }
        });
    }

}


// MainPresenterImpl class
public class MainPresenterImpl implements MainContract.presenter, MainContract.GetNoticeIntractor.OnFinishedListener{
    private MainContract.MainView mainView;
    private MainContract.GetNoticeIntractor getNoticeIntractor;

    public MainPresenterImpl(MainActivity mainActivity,  MainContract.GetNoticeIntractor getNoticeIntractor) {
        this.mainView = mainView;
        this.getNoticeIntractor = getNoticeIntractor;
    }
    @Override
    public void onDestroy() {
        mainView = null;
    }
    @Override
    public void onRefreshButtonClick() {
        if(mainView != null){
            mainView.showProgress();
        }
        getNoticeIntractor.getNoticeArrayList(this);
    }
    @Override
    public void requestDataFromServer() {
        getNoticeIntractor.getNoticeArrayList(this);
    }
    @Override
    public void onFinished(ArrayList<ApiObject> noticeArrayList) {
        if(mainView != null){
            Log.i("Okkkk:", "+++ "+ noticeArrayList);
             mainView.setDataToRecyclerView(noticeArrayList);
             mainView.hideProgress();
        }
    }
    @Override
    public void onFailure(Throwable t) {
        if(mainView != null){
            mainView.onResponseFailure(t);
            mainView.hideProgress();
        }
    }



// class model : ApiObject
public class ApiObject {
    @SerializedName("title")
    private String title;
    @SerializedName("description")
    private String description;
    public ApiObject(String title, String description) {
        this.title = title;
        this.description = description;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
}


// NoticeList class:
public class NoticeList {
    @SerializedName("notice_list")
    private ArrayList<ApiObject> noticeList;
    public ArrayList<ApiObject> getNoticeArrayList() {
        return noticeList;
    }
    public void setNoticeArrayList(ArrayList<ApiObject> noticeArrayList) {
        this.noticeList = noticeArrayList;
    }
}

// MainContract interface

public interface MainContract {
    /**
     * Call when user interact with the view and other when view OnDestroy()
     * */
    interface presenter{
        void onDestroy();
        void onRefreshButtonClick();
        void requestDataFromServer();
    }
    /**
     * showProgress() and hideProgress() would be used for displaying and hiding the progressBar
     * while the setDataToRecyclerView and onResponseFailure is fetched from the GetNoticeInteractorImpl class
     **/
    interface MainView {
        void showProgress();
        void hideProgress();
        void setDataToRecyclerView(ArrayList<ApiObject> noticeArrayList);
        void onResponseFailure(Throwable throwable);
    }
    /**
     * Intractors are classes built for fetching data from your database, web services, or any other data source.
     **/
    interface GetNoticeIntractor {
        interface OnFinishedListener {
            void onFinished(ArrayList<ApiObject> noticeArrayList);
            void onFailure(Throwable t);
            //void onFinished(String description);
        }
        void getNoticeArrayList(OnFinishedListener onFinishedListener);
    }
}

0 个答案:

没有答案