Android - 动态向Spinners添加值

时间:2012-08-04 16:57:31

标签: android spinner

我是Android开发的新手,所以查询可能看起来像新手。

我试图创建一个有几个微调器的应用程序,在用户选择一个微调器之后,另一个微调器根据spinner1中的选择填充数据。

即。第一个用户选择国家。根据国家,微调器2充满状态,并根据状态,微调器3充满了城市。

我已将所有国家/地区创建为strings.xml中的字符串数组,如 -

<string-array name="Country">
    <item >USA</item> etc...

类似地,已在string.xml中创建状态为 - (每个国家/地区是一个单独的条目,其命名约定为(国家/地区名称)_(状态)

<string-array name="USA_state">
    <item >New York</item> etc...

现在我希望第二个微调器基于国家/地区选择进行填充。因此,我使用的公式是得到第二个微调器 -

String getstate = country[index]+"_"+"state";
state=getResources()
.getStringArray(getResources().
getIdentifier(getstate,null,getPackageName()));

当我运行这个应用程序时,Manactivity只显示一个空白屏幕,我没有得到任何调试信息并得到一个错误“jar文件android.jar没有源附加”。

请帮忙。完整的mainActivity如下 -

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
import android.view.View; 

public class MainActivity extends Activity {
String[] country;
String[] state;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    country = getResources().getStringArray(R.array.country); 
    Spinner country_spinner =(Spinner) findViewById(R.id.country); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, country);
    country_spinner.setAdapter(adapter); 
    country_spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
        public void onItemSelected (AdapterView<?> arg0, View arg1, int arg2, long arg3){
            int index = arg0.getSelectedItemPosition();
            Toast.makeText(getBaseContext(), "You have Selected"+country[index]+"country", Toast.LENGTH_SHORT).show();
            Fill (index);
        }
        public void onNothingSelected(AdapterView<?> arg0) {}
    });


}

protected void Fill(int index) {
    // TODO Auto-generated method stub
    //Field resField=R.array.getField(country[index]);
    //int getstate = resField.getInt(null);
    String getstate = country[index]+"_"+"state";
    state=getResources().getStringArray(getResources().getIdentifier(getstate,null,getPackageName()));
    Spinner state_spinner = (Spinner) findViewById(R.id.state);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, state);
    state_spinner.setAdapter(adapter);
    state_spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3){
            int index2 = arg0.getSelectedItemPosition();
            Toast.makeText(getBaseContext(),"You Have Selected "+state[index2] + " statet",Toast.LENGTH_SHORT).show();
        }
        public void onNothingSelected(AdapterView<?> arg0){}
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

1 个答案:

答案 0 :(得分:2)

我能够解决问题。最终代码如下。这是一个嵌套的方法,所以如果有3个微调器(比如城市之后的状态),我们是否再次为该微调器嵌套代码?有没有更简单的方法?

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, country);
    country_spinner.setAdapter(adapter); 
    country_spinner.setOnItemSelectedListener(new OnItemSelectedListener(){

        public void onItemSelected (AdapterView<?> arg0, View arg1, int arg2, long arg3){
            int index = arg0.getSelectedItemPosition();
            Toast.makeText(getBaseContext(), "You have Selected"+country[index]+" County", Toast.LENGTH_SHORT).show();

            final CharSequence[] state_array = state.getTextArray(index);
            final Spinner state_spinner =(Spinner) findViewById(R.id.state); 
            ArrayAdapter<CharSequence> adapter1 = new ArrayAdapter<CharSequence>(MainActivity.this, android.R.layout.simple_spinner_item, state_array);
            state_spinner.setAdapter(adapter1); 
            state_spinner.setOnItemSelectedListener(new OnItemSelectedListener(){

                public void onItemSelected (AdapterView<?> arg0, View arg1, int arg2, long arg3){
                    final int index = arg0.getSelectedItemPosition();
                    Toast.makeText(getBaseContext(), "You have Selected"+state_array[index]+" State", Toast.LENGTH_SHORT).show();

        });

                }
                public void onNothingSelected(AdapterView<?> arg0) {
                    state_spinner.setAdapter(null);
                }
            });