纺纱厂如何知道它的价值?

发布于 2024-12-11 04:24:34 字数 2824 浏览 4 评论 0原文

我有一个微调器,它在值更改时启动第二个活动。这效果很好。

返回后一切也都正常。但是,如果我现在旋转移动设备,则会调用 OnItemSelectedListener!为什么?旋转器应位于位置 -1。这意味着回调不会被调用。

Android 如何知道选择了哪个微调项目?我尝试通过向基类发送savedInstanceState 的空值来阻止恢复此信息。

这是一些代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(null);
    setContentView(R.layout.menu);
    String db=MyDatabase.getData();

    if(db == null)
        db="";
    Spinner spinner=(Spinner)findViewById(R.id.spinner);
    MyAdapter adapter=new MyAdapter(this, db, "ID", "Name");
    spinner.setAdapter(adapter);
    adapter.setHint(spinner, "Please select...");
    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id2) {
            int id=((SpinnerItem)parent.getAdapter().getItem(position)).getId();
            Intent intent=new Intent();
            intent.setClass(MainMenu.this, Other.class);
            intent.putExtra("id", id);
            startActivity(intent);
        }

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

这是我的适配器:

public class MyAdapter extends ArrayAdapter<SpinnerItem> {
    private final Context context;
    private String hint;

    public MyAdapter(Context context, String input, String key, String value) {
        super(context, android.R.layout.simple_spinner_item, create(input, key, value));
        this.context=context;
    }

    private static SpinnerItem[] create(String input, String key, String value) {
        Vector<SpinnerItem> list=new Vector<SpinnerItem>();
        // fill the list
        return list.toArray(new SpinnerItem[] {});
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // show the hint
        if(position == -1) {
            TextView tv=new TextView(context);
            tv.setTextColor(Color.DKGRAY);
            tv.setText(hint);
            return tv;
        }
        return super.getView(position, convertView, parent);
    }

    public void setHint(Spinner spinner, String hint) {
        if(spinner.getAdapter() == null) {
            throw new IllegalStateException("Set your adapter first!");
        }
        this.hint=hint;
        try {
            final Method m=AdapterView.class.getDeclaredMethod("setNextSelectedPositionInt", int.class);
            m.setAccessible(true);
            m.invoke(spinner, -1);

            final Method n=AdapterView.class.getDeclaredMethod("setSelectedPositionInt", int.class);
            n.setAccessible(true);
            n.invoke(spinner, -1);
        } catch(Exception e) {
            throw new RuntimeException(e);
        }
    }
}

I have a spinner which starts a second activity on a value change. This works good.

After a return everything works fine too. But if I rotate now the mobile the OnItemSelectedListener is called! Why? The spinner should be on position -1. Which means that the callback will not be called.

How does android know which spinner item is selected? I tried to prevend to restore this information by sending the base class the a null value for the savedInstanceState.

Here is some code:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(null);
    setContentView(R.layout.menu);
    String db=MyDatabase.getData();

    if(db == null)
        db="";
    Spinner spinner=(Spinner)findViewById(R.id.spinner);
    MyAdapter adapter=new MyAdapter(this, db, "ID", "Name");
    spinner.setAdapter(adapter);
    adapter.setHint(spinner, "Please select...");
    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id2) {
            int id=((SpinnerItem)parent.getAdapter().getItem(position)).getId();
            Intent intent=new Intent();
            intent.setClass(MainMenu.this, Other.class);
            intent.putExtra("id", id);
            startActivity(intent);
        }

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

Here is my adapter:

public class MyAdapter extends ArrayAdapter<SpinnerItem> {
    private final Context context;
    private String hint;

    public MyAdapter(Context context, String input, String key, String value) {
        super(context, android.R.layout.simple_spinner_item, create(input, key, value));
        this.context=context;
    }

    private static SpinnerItem[] create(String input, String key, String value) {
        Vector<SpinnerItem> list=new Vector<SpinnerItem>();
        // fill the list
        return list.toArray(new SpinnerItem[] {});
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // show the hint
        if(position == -1) {
            TextView tv=new TextView(context);
            tv.setTextColor(Color.DKGRAY);
            tv.setText(hint);
            return tv;
        }
        return super.getView(position, convertView, parent);
    }

    public void setHint(Spinner spinner, String hint) {
        if(spinner.getAdapter() == null) {
            throw new IllegalStateException("Set your adapter first!");
        }
        this.hint=hint;
        try {
            final Method m=AdapterView.class.getDeclaredMethod("setNextSelectedPositionInt", int.class);
            m.setAccessible(true);
            m.invoke(spinner, -1);

            final Method n=AdapterView.class.getDeclaredMethod("setSelectedPositionInt", int.class);
            n.setAccessible(true);
            n.invoke(spinner, -1);
        } catch(Exception e) {
            throw new RuntimeException(e);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自由如风 2024-12-18 04:24:34

当您旋转设备时,您的 Activity 会重新启动或重新创建,请将此行添加到 Activity 标记中的 AndroidManifest 文件中以避免出现这种情况。

android:configChanges="keyboardHidden|orientation"

When you rotate your device your Activity gets restarted or recreated add this line to your AndroidManifest file in activity tag to avoid it.

android:configChanges="keyboardHidden|orientation"
一笑百媚生 2024-12-18 04:24:34

当您更改方向时,将调用 Activity 的 onCreate()

默认作为微调器的行为,每当创建 Activity 时,都会调用微调器的 onItemSelectedListener() 方法

因此,在这种情况下,您会遇到这种情况

此处给出了此类问题的解决方案StackOverflow问题的链接 检查他如何通过两个计数器处理问题 checkBoxCounter 和 checkBoxInitialized 并尝试在您的情况下实现相同的功能。

When you change the orientation the onCreate() of Activity get Called.

and by default as the behaviour of the spinner the method onItemSelectedListener() get called of the spinner when ever the activity gets created.

So this is happening to you in this case

Solution for such issue is given here StackOverflow Question's Link check how he handled the issue by taking two counters checkBoxCounter and checkBoxInitialized and try to implement same in your case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文