在Android的动态纱厂(一般工作流程问题) [英] Dynamic Spinners in Android (General Workflow Question)

查看:66
本文介绍了在Android的动态纱厂(一般工作流程问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像这样<一个href="http://stackoverflow.com/questions/4492003/dynamic-update-of-spinner2-based-on-choice-in-spinner-1">one,我见过几个怎么对的上这个网站,但说实话,我真的不的得到它的。

我想要一个微调的内容将基于一个previous飞旋的选择,就像在一个国家和城市的场景。总体而言,什么是工作流?是第二离心器基于所述第一旋转器过滤,或在第二离心器指向完全不同的列表基于第一离心器?结果

有关我自己的简单的学习项目,我已经建立了几个字符串数组中的strings.xml(AL-城市,AK-城市,AR-城市,等等)。我想这个城市微调的基础上,从国家微调选择正确的阵列来填充。但我想,如果不是我应该有一个有状态的缩写作为附加标识为城市的一个大的多维数组,然后在点第二微调,使用国家缩写作为过滤器。这似乎像前会提供更好的性能。

任何帮助(与code的例子)将大大AP preciated。我没有新的节目(PHP的居多,所以我猜的脚本更准确),但我是新来的Java。我的code迄今未链接的纺纱低于与第二微调指向一个未分化city_array。

感谢您!

 公共类例1扩展活动{

/ **第一次创建活动时调用。 * /
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.example1);

   微调spinState =(微调)findViewById(R.id.spin_state);
    ArrayAdapter&LT; CharSequence的&GT;适配器3 = ArrayAdapter.createFromResource(
            对此,R.array.state_array,android.R.layout.simple_spinner_item);
    adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinState.setAdapter(适配器3);

    微调spinCity =(微调)findViewById(R.id.spin_city);
    ArrayAdapter&LT; CharSequence的&GT; adapter4 = ArrayAdapter.createFromResource(
            对此,R.array.city_array,android.R.layout.simple_spinner_item);
    adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinCity.setAdapter(adapter4);
}
}
 

解决方案

您可以尝试从你的第一个微调,您所选择的一送的位置,然后根据该位置获取合适的阵列后,填充你的第二个微调

你要听你的第一个适配器被更改:

 微调。 setOnItemSelectedListener(新MyOnItemSelectedListener());


类MyOnItemSelectedListener实现OnItemSelectedListener {
    公共无效onItemSelected(适配器视图&LT;&GT;父,
        视图中查看,INT POS,长I​​D){
        字符串的选择= parent.getItemAtPosition(POS)的ToString();
            populateSecondSpinnerMethod(选择)
       }
    }
    公共无效onNothingSelected(适配器视图父){//什么也不做。
    }
}
 

Like this one, I've seen a few how-to's on this site, but truthfully, I'm not really getting it.

I want one spinner's content to be based upon a previous spinner selection, like in a States and Cities scenario. In general terms, what is the workflow? Are the results of the second spinner filtered based on the first spinner, or is the second spinner pointing to a completely different list based on the first spinner?

For my own simple learning project, I've built several string-arrays in the strings.xml (AL-Cities, AK-Cities, AR-Cities, etc). I'd like the city spinner to populate from the correct array based on a choice from the state spinner. But I'm wondering if instead I should just have one large multidimensional array of "Cities" that have a state abbreviation as an additional identifier, and then point the second spinner at that using the state abbreviation as a filter. It would seem like the former would provide better performance.

Any help (and code examples) would be greatly appreciated. I'm not new to programming (php mostly, so I guess scripting is more accurate), but I am new to java. My code so far with the spinners not linked is below with the second spinner pointing to an undifferentiated city_array.

Thank you!

public class Example1 extends Activity {

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.example1);

   Spinner spinState = (Spinner) findViewById(R.id.spin_state);
    ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(
            this, R.array.state_array, android.R.layout.simple_spinner_item);
    adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinState.setAdapter(adapter3);

    Spinner spinCity = (Spinner) findViewById(R.id.spin_city);
    ArrayAdapter<CharSequence> adapter4 = ArrayAdapter.createFromResource(
            this, R.array.city_array, android.R.layout.simple_spinner_item);
    adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinCity.setAdapter(adapter4);
}
}

解决方案

You could try and get the position from your first spinner, the one you've selected and then populate your second spinner after retrieving the proper array based on that position.

You have to listen for your first adapter being changed:

    spinner. setOnItemSelectedListener(new MyOnItemSelectedListener());


class MyOnItemSelectedListener implements OnItemSelectedListener {
    public void onItemSelected(AdapterView<?> parent,
        View view, int pos, long id) {
        String choice = parent.getItemAtPosition(pos).toString();
            populateSecondSpinnerMethod(choice)
       }
    }
    public void onNothingSelected(AdapterView parent) {       // Do nothing.
    }
}

这篇关于在Android的动态纱厂(一般工作流程问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆