简体   繁体   English

如何在Android中动态创建类

[英]How to create dynamically class in Android

In my application, I should useMaterial Stepper and for this, I want to use this library : https://github.com/ernestoyaquello/VerticalStepperForm在我的应用程序中,我应该使用Material Stepper ,为此,我想使用这个https : //github.com/ernestoyaquello/VerticalStepperForm

But I want to add this dynamically from server.但我想从服务器动态添加它。
For connecting with server I used Retrofit library and I should check the type of items from server.为了与服务器连接,我使用了Retrofit库,我应该检查来自服务器的项目类型。

when this type is " penny " show one of this steps and when the type is " best " show another step.当此类型为“便士”时,显示此步骤之一,当类型为“最佳”时,显示另一步。

I create this steps from library tutorials, but i want when type is penny show me StepDynamicTxt and when the type is best show me StepDynamicEdt !我从库教程中创建了这些步骤,但是我想要当类型是便士时向我展示StepDynamicTxt并且当类型最好时向我展示StepDynamicEdt

I write below codes but just add one of the items from each step!我写了下面的代码,但只需添加每一步中的一个项目!
But in API, I have 2 item of penny types and 3 items of best type!但是在 API 中,我有 2 项便士类型和 3 项最佳类型!

Should show me 5 step, but show me 2 step!应该给我看 5 步,但给我看 2 步!

My codes :我的代码:

public class StepperActivity extends AppCompatActivity {

    private ApiServices apiServices;
    private ProgressBar loader;
    private VerticalStepperFormView stepper;

    private StepDynamicEdt stepDynamicEdt;
    private StepDynamicTxt stepDynamicTxt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bidzila_stepper);
        //Initialize
        apiServices = ApiClient.ApiClient().create(ApiServices.class);
        loader = findViewById(R.id.bidStepper_loader);
        stepper = findViewById(R.id.bidStepper);

        //Api
        callAPi();
    }

    private void callAPi() {
        loader.setVisibility(View.VISIBLE);
        Call<TodayResponse> call = apiServices.TODAY_RESPONSE_CALL(5);
        call.enqueue(new Callback<TodayResponse>() {
            @Override
            public void onResponse(Call<TodayResponse> call, Response<TodayResponse> response) {
                if (response.isSuccessful()) {
                    if (response.body() != null) {
                        if (response.body().getRes() != null) {
                            if (response.body().getRes().getToday().size() > 0) {
                                loader.setVisibility(View.GONE);
                                //Foreach
                                for (int i = 0; i < response.body().getRes().getToday().size(); i++) {

                                    if (response.body().getRes().getToday().get(i).getType().equals("penny")) {

                                        stepDynamicEdt = new StepDynamicEdt(response.body().getRes().getToday().get(i).getName());

                                    } else if (response.body().getRes().getToday().get(i).getType().equals("best")) {

                                        stepDynamicTxt = new StepDynamicTxt(response.body().getRes().getToday().get(i).getName());
                                    }
                                }

                                stepper.setup(new StepperFormListener() {
                                    @Override
                                    public void onCompletedForm() {

                                    }

                                    @Override
                                    public void onCancelledForm() {

                                    }
                                }, stepDynamicEdt, stepDynamicTxt)
                                        .allowNonLinearNavigation(false)
                                        .displayCancelButtonInLastStep(false)
                                        .displayBottomNavigation(false)
                                        .confirmationStepTitle("Confirm")
                                        .stepNextButtonText("Continue")
                                        .lastStepNextButtonText("Finish")
                                        .includeConfirmationStep(false)
                                        .init();
                            }
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<TodayResponse> call, Throwable t) {
                Log.e("ResponseErr", t.getMessage());
            }
        });
    }

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(ViewPumpContextWrapper.wrap(newBase));
    }
}

I think this problem for this line: }, stepDynamicEdt, stepDynamicTxt) because just add 2 step.我认为这一行有这个问题: }, stepDynamicEdt, stepDynamicTxt)因为只需添加 2 步。

How can i add this step dynamically in Android?如何在 Android 中动态添加此步骤?

In your code, you are making a very fundamental mistake.在您的代码中,您犯了一个非常基本的错误。 And that is, you are using the same variable each time in your loop to store dynamic edit type and dynamic text type, which will replace any previously created fields.也就是说,您每次在循环中都使用相同的变量来存储动态编辑类型和动态文本类型,这将替换之前创建的任何字段。 And hence when you finally create them, you end up with single last values of each type.因此,当您最终创建它们时,您最终会得到每种类型的最后一个值。

What you can do is, create a List with type Step , add new type every time you get them, and finally pass that list to the builder.您可以做的是,创建一个类型为Step的 List ,每次获取它们时添加新类型,最后将该列表传递给构建器。

The builder accepts a list too, you should check implementation when its open source. 构建器也接受列表,您应该在其开源时检查实现。

// before the for loop, create a list of type Step
List<Step> steps = new ArrayList();
// your loop on response received from server
for (int i = 0; i < response.body().getRes().getToday().size(); i++) {
    if (response.body().getRes().getToday().get(i).getType().equals("penny")) {
        StepDynamicEdt stepDynamicEdt = new StepDynamicEdt(response.body().getRes().getToday().get(i).getName());
        // add to list
        steps.add(stepDynamicEdt);
    } else if (response.body().getRes().getToday().get(i).getType().equals("best")) {
        StepDynamicTxt stepDynamicTxt = new StepDynamicTxt(response.body().getRes().getToday().get(i).getName());
        // add to list
        steps.add(stepDynamicTxt);
    }
}
// finally create them
stepper.setup(new StepperFormListener() {
    @Override
    public void onCompletedForm() {

    }

    @Override
    public void onCancelledForm() {

    }
}, steps) // pass the list
        .allowNonLinearNavigation(false)
        .displayCancelButtonInLastStep(false)
        .displayBottomNavigation(false)
        .confirmationStepTitle("Confirm")
        .stepNextButtonText("Continue")
        .lastStepNextButtonText("Finish")
        .includeConfirmationStep(false)
        .init();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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