如何从此片段中的onItemSelectedListener中获取微调器单击的项?

时间:2017-07-27 01:59:56

标签: android listener spinner android-arrayadapter onitemselectedlistener

我有以下代码。我希望onItemSelectedListener将所选字符串的值放入store_id以进行进一步处理。我已经使用Toast来检查它,因为我无法在监听器上放置监视器,而store_id是默认的null。微调器显示我的各种弦,但点击时消失。有任何想法吗?我无法想象我的问题是将这个片段中的所有代码放在一起...... output right before clicking on a store

public class ChooseStoreFragment extends Fragment implements AdapterView.OnItemSelectedListener {
ArrayList<String> store_array = new ArrayList<>();
String store_id;

//implementing the AdapterView.OnItemSelectedListener
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    String user_store_id = parent.getItemAtPosition(position).toString();//doesn't show up in toast
    store_id = parent.getSelectedItem().toString();//doesn't show up in toast
    Toast.makeText(getActivity(), user_store_id + "=user_store_id.", Toast.LENGTH_SHORT).show();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
    Toast.makeText(parent.getContext(),"Nothing selected",Toast.LENGTH_SHORT).show();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View thisView = inflater.inflate(fragment_choose_store, container, false);
    final Spinner spinner = (Spinner) thisView.findViewById(R.id.stores_spinner);
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this.getContext(),android.R.layout.simple_spinner_dropdown_item,store_array);
    spinner.setAdapter(adapter);
    AdapterView.OnItemSelectedListener mListener = new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            store_id = parent.getSelectedItem().toString();
        } //still never shows up in toast

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            Toast.makeText(getActivity(), "none found = user_store_id.", Toast.LENGTH_SHORT).show(); //still nothing
        }
    };
    spinner.setOnItemSelectedListener(mListener);

1 个答案:

答案 0 :(得分:0)

您已为spinner注册了一位听众,因此您无需再次ChooseStoreFragment实施listener

 spinner.setOnItemSelectedListener(mListener);

应该是这样的

public class ChooseStoreFragment extends Fragment {
    ArrayList<String> store_array = new ArrayList<>();
    String store_id;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final View thisView = inflater.inflate(fragment_choose_store, container, false);
        final Spinner spinner = (Spinner) thisView.findViewById(R.id.stores_spinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this.getContext(), android.R.layout.simple_spinner_dropdown_item, store_array);
        spinner.setAdapter(adapter);
        AdapterView.OnItemSelectedListener mListener = new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                store_id = parent.getSelectedItem().toString();
            } //still never shows up in toast

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                Toast.makeText(getActivity(), "none found = user_store_id.", Toast.LENGTH_SHORT).show(); //still nothing
            }
        };
        spinner.setOnItemSelectedListener(mListener); // Register this spinner for a mListener
    }
}

或者,如果您想为您的班级注册听众

public class ChooseStoreFragment extends Fragment implements AdapterView.OnItemSelectedListener {
    ArrayList<String> store_array = new ArrayList<>();
    String store_id;

    //implementing the AdapterView.OnItemSelectedListener
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String user_store_id = parent.getItemAtPosition(position).toString();//doesn't show up in toast
        store_id = parent.getSelectedItem().toString();//doesn't show up in toast
        Toast.makeText(getActivity(), user_store_id + "=user_store_id.", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        Toast.makeText(parent.getContext(), "Nothing selected", Toast.LENGTH_SHORT).show();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final View thisView = inflater.inflate(fragment_choose_store, container, false);
        final Spinner spinner = (Spinner) thisView.findViewById(R.id.stores_spinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this.getContext(), android.R.layout.simple_spinner_dropdown_item, store_array);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);// register this fragment with a listener
    }
}

只注册一名听众!

更新:

public class ChooseStoreFragment extends Fragment implements AdapterView.OnItemSelectedListener {
    ArrayList<String> store_array = new ArrayList<>();
    String store_id;

    //implementing the AdapterView.OnItemSelectedListener
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String user_store_id = store_array.get(position);
        Toast.makeText(getActivity(), user_store_id, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        Toast.makeText(getActivity(), "Nothing selected", Toast.LENGTH_SHORT).show();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final View thisView = inflater.inflate(R.layout.fragment_choose_store, container, false);
        final Spinner spinner = (Spinner) thisView.findViewById(R.id.stores_spinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this.getActivity(), android.R.layout.simple_spinner_dropdown_item, store_array);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);// register this fragment with a listener
        return thisView;
    }
}