1

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

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

IE。第一个用户选择国家。根据国家,微调器 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;
}
4

1 回答 1

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);
                }
            });
于 2012-08-12T09:32:45.270 回答