1 year ago

#309343

test-img

diveincode

Multiple denpendent Spinner in Android

In my app there is a Spinner for colors and a Button for the user to add additional spinners in different colors. An existing spinner is coupled with a delete button for removal upon creation. It looks like this -

enter image description here

I'm trying to accomplish the following:

  • The user clicks "ADD PAINTYPE"
  • A Spinner is created, given it is not already present in the List
  • If the user deletes the item, it is available to insert again.

I´m pretty new to Android App development, and this logic is difficult for me to work through. Any suggestions on how to approach this would be appreciated.

Edit: Blow down is the function, which called after user click the button "ADD PAINTYPE". The arrayAdapter of the newly created spinner is as same as the fisrt one(which in the left side of the "ADD PAINTYPE" button). Right now all Spinners are sharing the sam adapter. What I want to achieve is, When I choose a Option in one spinner, then dynamiclly the option will not be listed in other spinners.(e.g. The "ANFALLSTARTIGER" would not be listed in the second and the third spinners). Is this possible?

 @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    protected void addItem(Context context,View view) {
        if(clickedTimes < 16) {
            clickedTimes++;
            final LinearLayout linearLayout = new LinearLayout(context);

            Spinner spinner = new Spinner(context);
            spinner.setPopupBackgroundResource(R.color.white);
            spinner.setBackgroundColor(getResources().getColor(R.color.white));
            final Button button = new Button(context);
            final Button button_delete = new Button(context);
            button_delete.setText("DELETE");
            button_delete.setTextColor(getResources().getColor(R.color.white));
            button_delete.setBackground(context.getDrawable(R.drawable.mybutton));
            linearLayout.setOrientation(LinearLayout.HORIZONTAL);
            linearLayout.setWeightSum(6);
            LinearLayout.LayoutParams params_0 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            linearLayout.setLayoutParams(params_0);
            LinearLayout.LayoutParams params_1 = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 4.0f);
            spinner.setLayoutParams(params_1);
            params_1.setMargins(2, 2, 2, 2);
            LinearLayout.LayoutParams params_2 = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
            params_2.setMargins(2, 2, 2, 2);
            button.setLayoutParams(params_2);
            button_delete.setLayoutParams(params_2);

            LinearLayout layout = (LinearLayout) view.findViewById(R.id.all_paintype);

            layout.addView(linearLayout);
            linearLayout.addView(spinner);
            linearLayout.addView(button);
            linearLayout.addView(button_delete);
            button_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    LinearLayout layoutAll = (LinearLayout) view.findViewById(R.id.all_paintype);
                    layoutAll.removeView(linearLayout);
                    spinners.remove(spinner);
                    clickedTimes--;
                }
            });
            dropBoxButton(spinner, button, clickedTimes);
            spinners.add(spinner);
        }else {
            Toast toast = Toast.makeText(getContext(),"Maximal Pain Type",Toast.LENGTH_SHORT);
            toast.show();
        }
    }

    protected void dropBoxButton(Spinner spinner, final Button button, int clickedTimes) {
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(arrayAdapter);
        spinner.setSelection(clickedTimes);

        spinner.setVisibility(View.VISIBLE);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String string_1 = ((TextView) view).getText().toString();
                setButtonColor(button, string_1);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }

java

android

spinner

0 Answers

Your Answer

Accepted video resources