Frage

I have a spinner that when selected should add a new spinner to the view. But when I try an inflate it I am getting an error on this line:

LinearLayout addSpinner =(LinearLayout)selectedItemView.getContext().findViewById(R.id.addSpinnerLayout);

findViewByID is red and shows this error on mouse hover:

Cannot resolve method 'findViewById(int)'

My whole java class code is this:

public class Portfolio extends ActionbarMenu {



    //get beer details from bundle
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_portfolio);

        final Spinner portfolioType = (Spinner) findViewById(R.id.portfolioSpinner);
        portfolioType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {


            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {

                String portfolioChoice = portfolioType.getSelectedItem().toString();

                Toast.makeText(getApplicationContext(), portfolioChoice, Toast.LENGTH_LONG).show();

                Log.d("portfolio" , portfolioChoice);

                if( portfolioChoice.equals("All")){
                    //get userID
                    //get user data
                    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(selectedItemView.getContext());
                    String userID = prefs.getString("userID", null);

                    //construct url
                    String url = "myURL"


                    //async task goes here
                    new PortfolioGetAllBeers(selectedItemView.getContext()).execute(url);

                }

                else if (portfolioChoice.equals("Brewery")){

                    //inflate brewery spinner
                    //to do: change to start rater
                    LinearLayout ll = (LinearLayout) findViewById(R.id.addSpinnerLayout);
                    ll.removeAllViews();

                    //add spinner to layout
                    LayoutInflater inflater = (LayoutInflater)selectedItemView.getContext().getSystemService(selectedItemView.getContext().LAYOUT_INFLATER_SERVICE);
                    LinearLayout addSpinner = (LinearLayout)selectedItemView.getContext().findViewById(R.id.addSpinnerLayout);
                    addSpinner.addView(inflater.inflate(R.layout.addspinner_layout, null));

                    //todo: get breweries and fill spinner


                }








            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // your code here
            }

        });



    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main2, menu);

        return true;
    }




}

activity_portfolio:

<?xml version="1.0" encoding="utf-8"?>


    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="6dp"
                android:layout_marginRight="6dp"
                android:layout_marginTop="4dp"
                android:layout_marginBottom="4dp"
                android:orientation="vertical"
                android:background="@drawable/bg_card">

                <!-- Card Contents go here -->
                <TextView
                    android:id="@+id/portfolioTitle"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:textSize="20sp"
                    android:textStyle = "bold"
                    android:text="Your Portfolio"
                    android:padding="5dip"
                    >
                </TextView>




            </LinearLayout >

        </FrameLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="6dp"
                android:layout_marginRight="6dp"
                android:layout_marginTop="4dp"
                android:layout_marginBottom="4dp"
                android:orientation="vertical"
                android:background="@drawable/bg_card">

                <!-- Card Contents go here -->
                <TextView
                    android:id="@+id/sortTitle"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:textSize="15sp"
                    android:textStyle = "bold"
                    android:text="Sorting Options:"
                    android:padding="5dip"
                    >
                </TextView>

                <LinearLayout
                    android:layout_height="wrap_content"
                    android:layout_width="match_parent"
                    android:orientation="horizontal"
                    >

                    <Spinner
                        android:id="@+id/portfolioSpinner"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:entries="@array/portfolio_array"
                        android:layout_weight="1"

                        />

                    <LinearLayout
                        android:id="@+id/addSpinnerLayout"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        android:layout_weight="1">

                    </LinearLayout>


                </LinearLayout>




            </LinearLayout >

        </FrameLayout>

    <ListView
        android:id="@+id/allYourBeersList"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:dividerHeight="0px"
        android:divider="@null"

        >
    </ListView>


    </LinearLayout>

addspinner_layout :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <Spinner
        android:id="@+id/portfolioSpinner2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/portfolio_array"
        android:layout_weight="1"

        />




</LinearLayout>
War es hilfreich?

Lösung

use this

  LinearLayout ll = (LinearLayout) findViewById(R.id.addSpinnerLayout);
  LayoutInflater inflater = (LayoutInflater)selectedItemView.getContext().getSystemService(selectedItemView.‌​getContext().LAYOUT_INFLATER_SERVICE) 
  View v = inflater.inflate(R.layout.addspinner_layout, null); // inflate addspinner
  Spinner sp = (Spinner) v.findViewById(R.id.portfolioSpinner2); //portfolioSpinner2 
  ll.addView(v); // add the view to the linear layout

You have this

<LinearLayout
      android:id="@+id/addSpinnerLayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      android:layout_weight="1">

 </LinearLayout>

I suppose you need to add the spinner to the linear layout.

So

 LinearLayout ll = (LinearLayout) findViewById(R.id.addSpinnerLayout);

Inflate the layout

  LayoutInflater inflater = (LayoutInflater)selectedItemView.getContext().getSystemService(selectedItemView.getContext().LAYOUT_INFLATER_SERVICE);
  View v = inflater.inflate(R.layout.addspinner_layout, null);  

Then add it to LinearLayout

  ll.addView(v); // add the view to the linear layout

To initialize Spinner

  Spinner sp = (Spinner) v.findViewById(R.id.portfolioSpinner2);

coz you have this

    <Spinner
    android:id="@+id/portfolioSpinner2" // in addSpinner_layout

Andere Tipps

Try instead

LinearLayout addSpinner = inflater.inflate(R.layout.addSpinnerLayout, null);

I'm a little confused on your syntax and what you are trying to do but this should fix that problem.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top