【问题标题】:Dynamically selecting one particular array from string.xml从 string.xml 中动态选择一个特定的数组
【发布时间】:2014-05-17 02:55:49
【问题描述】:

创建了一个应用,其中有两个 AutoCompleteTextView。在第一个用户输入他/她想要的食物种类。例如:意大利文、中文等。

如果假设用户在第一个 AutoCompleteTextView 中输入中文,那么第二个 AutoCompleteTextView 应该只建议中国食品。在 strings.xml 中,我创建了一个名为“tastes”的数组,其中包含意大利、中国等不同的口味。然后我有一个名为“Italian”的数组,其中仅包含意大利菜肴,就像一个名为“Chinese”的数组其中仅包含中国菜等。

如果一旦用户在第一个 textview 中输入了他的选择,我如何从 strings.xml 中为第二个 textview 选择相应的数组?

除了 swicth case,我还能用什么?

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

    this.text1 = (AutoCompleteTextView) findViewById(R.id.taste);
     String[] tastes = getResources().getStringArray(R.array.tastes); 
    ArrayAdapter<String> adapterTaste = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, taste);
    this.text1.setAdapter(adapterTaste);
    this.text1.setThreshold(1);



     this.text2 = (AutoCompleteTextView) findViewById(R.id.item);
     String[] items = getResources().getStringArray(R.array.Chinese);
     // In place of Chinese in the above line, the feed from text1 ie users choice. 
    ArrayAdapter<String> adapterItem = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, item);
    this.text2.setAdapter(adapterItem);
    this.text2.setThreshold(1);

        }

【问题讨论】:

    标签: android arrays autocomplete filtering


    【解决方案1】:

    setOnItemSelectedListener() 是您正在寻找的方法。

    在您的情况下,您的源代码将是,

    text1.setOnItemSelectedListener(new OnItemSelectedListener(){
        public void onItemClick(AdapterView<?> parent, View view, int position, long id){
            // Use the position to get which item was selected i.e Chinese or Italian
            // Create an adapter for the second AutoComplete
            // Set the adapter for second AutoCompleteTextView
        }
    });
    

    Method Documented here 希望这会有所帮助:)

    【讨论】:

    • 感谢您的特殊方法,请您建议如何设置适配器,因为数组已在 string.xml 中定义。
    猜你喜欢
    • 2019-09-23
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    • 2014-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多