【问题标题】:Set selection at beginning of spinner with simplecursoradapter使用简单光标适配器在微调器开始时设置选择
【发布时间】:2017-09-30 20:52:24
【问题描述】:

我有一个带有 listView 的活动 A。我里面有几样东西。如果我单击其中一个项目,我会使用 Intent 导航到另一个页面。在另一个 poage/Activity 中,我有一个微调器,其中包含 listView 的项目(填充了数据库)。问题是,每次我去那个活动 B 时,微调器中显示的项目总是我数据库中的第一个。如何根据我在 listView 中单击的项目设置要显示的项目?

这是我填充微调器的方式(下拉模式):

    spinner = (Spinner)findViewById(R.id.spinner_branches);
    final Cursor cursor = dbhelper.getAllCours();
    String[] from = { "branche_cours" }; int[] to = { android.R.id.text1 };
    adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, from, to, 0);
    spinner.setAdapter(adapter);
    adapter.notifyDataSetChanged();

当我导航时,我会传递带有意图的选定项目。我提醒您,我的 listView 包含与微调器相同的项目..

String selected_brancheold= intent.getExtras().getString("branche");

谢谢你们!

【问题讨论】:

    标签: android database spinner


    【解决方案1】:

    如果您的 ListView 列表和 Spinner List 都相同,您可以做一件事。

    活动 A

        Intent intent = new Intent(YOUR_CONTEXT, ActivityB.class);
        intent.putExtra("position",PASS_CURRENT_POSITION_LISTVIEW_ITEM_CLICKED);
        startActivity(intent);
    

    活动 B(设置微调器适配器后)

        int currentPosition = getIntent().getIntExtra("position",0);
        spinner.setSelection(currentPosition);
    

    希望这对你有用!!

    【讨论】:

    • 谢谢先生!有效 !另外,我可以再问你一件事:将来我会使用更多的微调器..我怎样才能在开始时设置“选择一个”的值?我也会使用 simplecursoraapter
    • 乐于助人!!这似乎是一个新问题。我建议您为此添加单独的问题。
    • 我现在在测试时看到它不正确。我的意思是,现在它一直返回最后一个值,你能帮帮我吗?我应该如何在我的 listView 上获得位置?
    • 好吧nvm,我想通了!感谢你们对我的帮助 !并为昨天的损失感到抱歉;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    • 1970-01-01
    • 1970-01-01
    • 2011-10-03
    • 1970-01-01
    • 2011-07-29
    相关资源
    最近更新 更多