Bjonic
Bjonic

Reputation: 192

Why isn't my spinner getting populated

So I have two classes, MainActivity and SpinnerActivity.

Whatever I do now, my spinner just doesn't get populated with the data from the topicsAdapter which uses the topics ArrayList.

Maybe it's just something easy i've stumbled upon, but I just can't figure it out.

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar myToolbar = findViewById(R.id.my_toolbar);
    setSupportActionBar(myToolbar);

    Intent spinner = new Intent(this, SpinnerActivity.class);
    startActivity(spinner);
}

SpinnerActivity.java

public class SpinnerActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LayoutInflater inflater = LayoutInflater.from(this);
    View view = inflater.inflate(R.layout.activity_main, null);


    ArrayList<String> topics = new ArrayList<>();

    topics.add("Home");
    topics.add("Android");
    topics.add("Test 3");

    Spinner spinner = (Spinner) view.findViewById(R.id.toolbar_spinner);
    ArrayAdapter<String> topicsAdapter = new ArrayAdapter<>(this, R.layout.spinner_item, topics);
    topicsAdapter.setDropDownViewResource(R.layout.spinner_dropdown);
    spinner.setAdapter(topicsAdapter);

}

Upvotes: 1

Views: 66

Answers (3)

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

Issue : you are creating a separate view (by inflating it) and has no connection with the layout of current SpinnerActivity and spinner is also being created in that unrelated view.

Solutions :

You can do setContentView(view); to use the spinner which is inside the inflated view

or

if you have spinner in your layout then use(seems like it is otherwise it would have been crashed)

Spinner spinner = (Spinner) findViewById(R.id.toolbar_spinner);

and remove

LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.activity_main, null);

Upvotes: 2

Anton Kazakov
Anton Kazakov

Reputation: 2764

You should change setContentView(R.layout.activity_main); to layout file where this Spinner located. BTW you should remove these lines

LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.activity_main, null);

Upvotes: 0

Shalu T D
Shalu T D

Reputation: 4039

Please replace your code with this :-

public class SpinnerActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ArrayList<String> topics = new ArrayList<>();

    topics.add("Home");
    topics.add("Android");
    topics.add("Test 3");

    Spinner spinner = (Spinner) findViewById(R.id.toolbar_spinner);
    ArrayAdapter<String> topicsAdapter = new ArrayAdapter<>(this, R.layout.spinner_item, topics);
    topicsAdapter.setDropDownViewResource(R.layout.spinner_dropdown);
    spinner.setAdapter(topicsAdapter);

}

Upvotes: 0

Related Questions