【问题标题】:Spinner OnItemSelectedListener not working on CustomListAdapterSpinner OnItemSelectedListener 不适用于 CustomListAdapter
【发布时间】:2016-04-16 16:01:19
【问题描述】:

我在不同的活动中有微调器来选择要显示的季度。为了尝试它是否有效,我只为测试做吐司,但它没有显示。

我将它应用到我的 CustomListAdapter。

FeedListAdapter 代码

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final int gradeid;
        final int subjectid;
        final int studentid;
        final String first;
        final String second;
        final String third;
        final String fourth;
        final String subjname;
        final String remrks;

        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.feed_item, null);

        if (imageLoader == null)
            imageLoader = AppController.getInstance().getImageLoader();

        final FeedItem item = feedItems.get(position);

        first2 = (TextView) convertView.findViewById(R.id.txtfirst2);
        first3 = (TextView) convertView.findViewById(R.id.txtfirst);

        LayoutInflater layoutInflater =
                (LayoutInflater) activity.getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View addView = layoutInflater.inflate(R.layout.activity_grades, null);

        final Spinner mySpinner = (Spinner) addView.findViewById(R.id.spinner);

        mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                String imc_met= mySpinner.getSelectedItem().toString();
                if (imc_met.equals("1")){
                    Toast.makeText(context, "1", Toast.LENGTH_SHORT).show();
                    //first3.setVisibility(View.GONE);
                    //first2.setVisibility(View.GONE);
                }


            }
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

return convertView;
    }

正如我在上面提到的 Toast 它没有显示。我的编码有什么问题?

欢迎任何帮助或建议!我真的需要你们的帮助!!

更新

这是我关于使用 Spinner 隐藏 textview 的另一篇文章。顺便说一句,我正在隐藏 CustomListAdapter 中的项目。

Set visibility of TextView from different Layout not working Android

【问题讨论】:

  • 您可以在调试模式下测试应用程序并在String imc_met= mySpinner.getSelectedItem().toString(); 行中设置断点,然后查看返回的值。我认为您的字符串返回“”而不是“1”。
  • @SadeqShajary 等我试试
  • 你也可以在 if 之前/不加 if 之前将 imc_met 放入 toast 中。
  • 我也相信您通常不会将 onItemSelected 放入 getView 中,因为列表中的每个项目都会调用 getView。因此,您将多次设置侦听器(不确定会产生什么影响)。就我个人而言,我将侦听器放在活动中而不是适配器中。
  • 都试过了,但都没有显示。

标签: android listview spinner


【解决方案1】:

在确定真正的问题是设置其他布局中定义的两个 TextView 的可见性之后。建议是使用共享偏好。在微调器的 OnItemSelect 侦听器中设置共享首选项并在相应的活动中检索它们。

以下代码来自How to set a default value to SharedPreferences programmatically?,用于设置 SharedPreference(s):-

    SharedPreferences prefs = getActivity().getSharedPreferences(
            PREFS_NAME, 0);
    if (prefs.getInt("key_weight", null) == null) {
        Editor editor = prefs.edit();
        editor.putInt("key_weight", 75);
        editor.commit();
    }

提供了有关检索 SharedPreferences 的以下代码:-

        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        final boolean devmode = sp.getBoolean(getResources().getString(R.string.sharedpreferencekey_developermode),false);

注意 R.string.sharedpreferencekey_developermode 拥有共享首选项键名

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多