Android的 - 动态添加值纱厂(Android - Dynamically Adding va

2019-09-20 19:19发布

我是新来的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只是显示一个空白的屏幕,我DONOT得到任何调试信息,并得到一个错误“的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;
}

Answer 1:

我能解决这个问题。 最终的代码如下所示。 这是一个嵌套的方式,所以如果有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);
                }
            });


文章来源: Android - Dynamically Adding values to Spinners