Skip to content
Advertisement

How to create dynamically class in Android

In my application, I should use Material Stepper and for this, I want to use this library : 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.

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!

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!

Should show me 5 step, but show me 2 step!

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.

How can i add this step dynamically in Android?

Advertisement

Answer

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.

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();
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement