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

Posted

技术标签:

【中文标题】如何从该片段中的 onItemSelectedListener 中获取微调器单击的项目?【英文标题】:How do I get the spinner clicked item out of the onItemSelectedListener in this fragment? 【发布时间】:2018-01-02 13:14:57 【问题描述】:

我有以下代码。我希望 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);

【问题讨论】:

你已经实现了两个AdapterView.OnitemSelectedListener。你要哪一个? 第一个。第二个实现只是尝试更多的局部性成为一个问题!? 【参考方案1】:

您已经为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;
    

【讨论】:

我喜欢你的代码,谢谢,漂亮又干净,但仍然没有从 onItemSelected 到 store_id 的数据。你能看看 parent.getSelectedItem() 行/方法吗? 因为您的 store_array 不包含任何内容 但确实如此,它由 VolleyRequest 加载,并且微调器显示数据。我的问题还不允许我输出的屏幕截图(低等级)。你看一下截图好吗? 我上次更新了,请考虑重写你的代码。 我想我会发布我想出的成功代码,#include 感谢Phat,它很难看,因为我不得不添加系统时钟延迟,但这对于应用程序来说是肤浅的。如果有的话,我会说考虑到这个例子,该语言还没有准备好从片段中实现用户定义的微调器。干杯!

以上是关于如何从该片段中的 onItemSelectedListener 中获取微调器单击的项目?的主要内容,如果未能解决你的问题,请参考以下文章

从android eclipse中的片段刷新或更新listadapter

从ViewPager android替换片段

如何在活动和浏览器片段之间传递值? [复制]

如何防止在android片段中按下后退按钮时调用onCreateView

从底部工作表对话框片段中获取价值

R:如何从该列表中的所有数据框中删除行?