Android Spinner Dialog is not Populating?

2019-08-23 22:56发布

I have an activity made up of two fragments. On the left is a sidebar, on the right is a MapView (See screenshot below).

On the left I have a spinner that will be populated with objects on the map. Right Now, I have it hardcoded with some values until I figure out this issue. When the Spinner is selected, a dialog appears, but it is empty. Any idea what is going on?

Here is my code for the left fragment (Its not very complex):

package android.splitdisplay;


import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

public class SideFragment extends Fragment {
    String[] tracks = { "ObjectOne", "ObjectTwo",
            "ObjectThree"

    };


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getActivity().setContentView(R.layout.sidefragment);

        final Spinner s = (Spinner) this.getActivity().findViewById(
                R.id.track_spinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this
                .getActivity().getBaseContext(),
                android.R.layout.simple_spinner_item, tracks);
        s.setAdapter(adapter);

        s.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                int item = s.getSelectedItemPosition();
                Toast.makeText(SideFragment.this.getActivity().getBaseContext(),
                        "clicked "+item, Toast.LENGTH_SHORT).show();
            }

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

    }

    public void onListItemClick(ListView parent, View v, int position, long id) {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.sidefragment, container, true);
    }
}

Here is the XML for the SideFragment:

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Tracks"
        android:textColor="#adff2f"
        android:textSize="30sp" />

    <Spinner
        android:id="@+id/track_spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="true"
        android:prompt="@string/track_prompt"
        android:spinnerMode="dialog" >
    </Spinner>

</LinearLayout>

Here is what I am seeing when I click on the spinner:

What I am seeing

Here is all of the source code in a zip:

http://db.tt/KMGXhZWc

2条回答
爷的心禁止访问
2楼-- · 2019-08-23 23:39

this is how I always have done my spinners

ArrayAdapter<CharSequence> cAdapter;
    cAdapter = ArrayAdapter.createFromResource(this, R.array.colors,android.R.layout.simple_spinner_item);
    int cSpinnerDD = android.R.layout.simple_spinner_dropdown_item;
    cAdapter.setDropDownViewResource(cSpinnerDD);
    color.setAdapter(cAdapter);

give that a try

查看更多
【Aperson】
3楼-- · 2019-08-23 23:47

The problem is not the Spinner, the problem is that you are inflating two Spinners. One in on onCreateView method and the other in this line :

this.getActivity().setContentView(R.layout.sidefragment);

And only one the spinner with the provider... so I recommend you to set all your GUI variable at onCreationView like this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.sidefragment, container, true);
    final Spinner s = (Spinner) v.findViewById(
            R.id.track_spinner);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this
            .getActivity().getBaseContext(),
            android.R.layout.simple_spinner_item, tracks);
    s.setAdapter(adapter);

    s.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            int item = s.getSelectedItemPosition();
            Toast.makeText(SideFragment.this.getActivity().getBaseContext(),
                    "clicked "+item, Toast.LENGTH_SHORT).show();
        }

            @Override
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    });
    return v;
}

And remove the overrided method onCreate.

查看更多
登录 后发表回答