是否具有的android这个功能"?如何筛选的基础上微调"?有的、基础上、功能、android

2023-09-05 04:59:01 作者:踩到姑娘滴小蘑菇

我想问一个非常普遍的问题。 如何基于微​​调过滤器? 意思是,有一个在微调几个选项(教育,博馆,餐厅) 在选择博物馆它会告诉我博馆的列表。 有没有这样一种方式来做到这一点? 我这些数据检索,这一点,我想知道是否微调可以做到这一点的功能。

我有谷歌,但不似乎找到了我想要的答案 因此,想寻求意见,是否有任何这样的来源。

在问题的变化,在某种程度上类似

如果我有这种编码,并希望取回或者博物馆,新加坡,或中心, 我应该如何在我的code编辑? 意思是,当点击选择的微调, 它会改变

MainActivity.java     公共类MainActivity扩展ListActivity {

 的String []种类= {
    新加坡发现中心,
    新加坡科学中心,
    造币博物馆,
    新加坡美术馆,
    军事博物馆
};

的String []关键字= {
    中央,
    博馆,
    新加坡,

};

微调S1;






    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);

        //网格视图
                setListAdapter(新ArrayAdapter<字符串>(这一点,android.R.layout.simple_list_item_1,类别));

                // SpinnerView
                S1 =(微调)findViewById(R.id.spinner1);

                ArrayAdapter<字符串>适配器=新的ArrayAdapter<字符串>(这一点,android.R.layout.simple_spinner_item,关键词);
                s1.setAdapter(适配器);
                s1.setOnItemSelectedListener(新OnItemSelectedListener()
                {
                    公共无效onItemSelected(适配器视图<>为arg0,查看ARG1,INT ARG2,长ARG3){
                        INT指数= s1.getSelectedItemPosition();
                        Toast.makeText(getBaseContext(),你已经入围项目:+关键字[指数],Toast.LENGTH_SHORT).show();
                    }
                    公共无效onNothingSelected(适配器视图<>为arg0){}
                    });
                }


        公共无效onListItemClick(ListView的父,视图V,INT位置,长ID)
        {
            Toast.makeText(这一点,你已经选择了+分类[位置],Toast.LENGTH_SHORT).show();
        }
        }
 
另一角度看Android Android 系统架构与 Linux 对比分析

解决方案

首先你应该实施OnItemSelectedListener

 微调SP;
    的String [] strarr = {教育,博馆,餐厅};


ArrayAdapter<字符串>适配器=新的ArrayAdapter<字符串>(filename.this,android.R.layout.simple_spinner_item,strarr);
        SP =(微调)findViewById(R.id.spinner1);
        sp.setAdapter(适配器);
        sp.setOnItemSelectedListener(本);
 

您应该重写

 公共无效onItemSelected(适配器视图<>为arg0,查看ARG1,INT ARG2,
            长ARG3){

        INT位置= sp.getSelectedItemPosition();
              //要做的事
}
和
公共无效onNothingSelected(适配器视图<>为arg0){

        //要做的事
    }
 

I would like to ask a very general question. " How to filter based on spinner?" Meaning, there's few option in spinner ("education", "musuem", "restaurant") Upon selecting "museum" it will show me a list of musuem. Is there such a way to do it? I've those data retrieved, just that, I would like to know whether spinner can do this function.

I've google it but doesn't seems to find the answer that I wanted thus, would like to seek for advice whether is there any such sources.

CHANGES IN QUESTION, SOMEHOW SIMILAR

If I've this coding, and wanted to retrieve either "Museum", "Singapore", or "Centre", How should I edit in my code? Meaning, upon click on the selection in spinner, it will change.

MainActivity.java public class MainActivity extends ListActivity {

    String[] Category = {
    "Singapore discovery Centre",
    "Singapore Science Centre",
    "Mint Museum",
    "Singapore Art Museum",
    "Army Museum"
};

String [] keywords = {
    "Centre",
    "Musuem",
    "Singapore",

};

Spinner s1;






    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //GridView
                setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,Category)); 

                //SpinnerView
                s1 = (Spinner) findViewById(R.id.spinner1);

                ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, keywords);
                s1.setAdapter(adapter);
                s1.setOnItemSelectedListener(new OnItemSelectedListener()
                {
                    public void onItemSelected(AdapterView<?> arg0,View arg1, int arg2, long arg3) {
                        int index = s1.getSelectedItemPosition();
                        Toast.makeText(getBaseContext(), "You have seleted item :" + keywords[index] , Toast.LENGTH_SHORT).show();
                    }
                    public void onNothingSelected(AdapterView<?>arg0) {}
                    });
                }


        public void onListItemClick(ListView parent, View v, int position,long id)
        {
            Toast.makeText(this, "You have selected " + Category[position], Toast.LENGTH_SHORT).show();
        }
        }

解决方案

first of all you should implement OnItemSelectedListener.

Spinner sp;
    String[] strarr={"education", "musuem", "restaurant"};


ArrayAdapter<String> adapter = new ArrayAdapter<String>(filename.this,android.R.layout.simple_spinner_item,strarr);
        sp=(Spinner) findViewById(R.id.spinner1);
        sp.setAdapter(adapter);
        sp.setOnItemSelectedListener(this);

you should override

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {

        int position =sp.getSelectedItemPosition();
              //things to do
}
and 
public void onNothingSelected(AdapterView<?> arg0) {

        //things to do
    }