Android的 - 动态添加值纱厂纱厂、动态、Android

2023-09-07 10:27:57 作者:木冇魚丸

我是新来的Andr​​oid开发这样的查询可能看起来新手。

我试图让一个应用程序,其中有一对夫妇纺纱和用户选择后的一个微调器中的其它微调获取与根据spinner1选择数据填满。

IE浏览器。首先用户选择国家。根据国家微调2被各国填平,在微调3获取与城市填补了基于状态的。

我创建的所有国家作为一个字符串数组在这样的strings.xml -

 <字符串数组名=国家>    <项目>美国< /项目>等等... 

同样的状态一直在string.xml创建为 - (每个国家是一个命名约定(国名)_(美国)

一个单独的条目

 <字符串数组名=USA_state>    <项目>纽约< /项目>等等... 

现在我想第二微调填充设在国家选择。因此,由我使用的公式是将获得第二个微调是 -

 字符串,有getstate =国家[指数] +_+状态;状态= getResources().getStringArray(getResources()。则getIdentifier(,有getstate,空,getPackageName())); 
android动态获取输入值,Android实现动态自动匹配输入的内容

当我运行这个应用程序中的Manactivity只是显示一个空白的屏幕,我DONOT得到任何调试信息,并得到一个错误的jar文件的android.jar不附带任何来源。

请帮忙。下面的完整mainActivity给出 -

 进口android.os.Bundle;进口android.app.Activity;进口android.view.Menu;进口android.view.MenuItem;进口android.widget.AdapterView;进口android.widget.AdapterView.OnItemSelectedListener;进口android.widget.ArrayAdapter;进口android.widget.Spinner;进口android.widget.Toast;进口android.support.v4.app.NavUtils;进口android.view.View;公共类MainActivity延伸活动{的String []国家;的String []状态;@覆盖公共无效的onCreate(捆绑savedInstanceState){    super.onCreate(savedInstanceState);    的setContentView(R.layout.activity_main);    。国家= getResources()getStringArray(R.array.country);    微调country_spinner =(微调)findViewById(R.id.country);    ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(这一点,android.R.layout.simple_spinner_item,国家);    country_spinner.setAdapter(适配器);    country_spinner.setOnItemSelectedListener(新OnItemSelectedListener(){        公共无效onItemSelected(适配器视图<>为arg0,ARG1观,诠释ARG2,长ARG3){            INT指数= arg0.getSelectedItemPosition();            Toast.makeText(getBaseContext(),选择了+国家[指数] +国家,Toast.LENGTH_SHORT).show();            填写(指数);        }        公共无效onNothingSelected(适配器视图<>为arg0){}    });}保护无效的填充(INT指数){    // TODO自动生成方法存根    //现场resField = R.array.getField(国家[指数]);    //有getstate INT = resField.getInt(NULL);    有getstate字符串全国= [指数] +_+状态;    。状态= getResources()getStringArray(getResources()则getIdentifier(,有getstate,空,getPackageName()));    微调state_spinner =(微调)findViewById(R.id.state);    ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(这一点,android.R.layout.simple_spinner_item,状态);    state_spinner.setAdapter(适配器);    state_spinner.setOnItemSelectedListener(新OnItemSelectedListener(){        公共无效onItemSelected(适配器视图<>为arg0,ARG1观,诠释ARG2,长ARG3){            INT索引2 = arg0.getSelectedItemPosition();            Toast.makeText(getBaseContext(),选择了+状态[索引2] +statet,Toast.LENGTH_SHORT).show();        }        公共无效onNothingSelected(适配器视图<>为arg0){}    });}@覆盖公共布尔onCreateOptionsMenu(菜单菜单){    。getMenuInflater()膨胀(R.menu.activity_main,菜单);    返回true;} 

解决方案

我是能够解决的问题。最终code为如下。这是一个嵌套的方式,所以如果有3个纺纱厂(如状态后,市)做我们再次窝code为微调?有没有更简单的方法?

  ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(这一点,android.R.layout.simple_spinner_item,国家);    country_spinner.setAdapter(适配器);    country_spinner.setOnItemSelectedListener(新OnItemSelectedListener(){        公共无效onItemSelected(适配器视图<>为arg0,ARG1观,诠释ARG2,长ARG3){            INT指数= arg0.getSelectedItemPosition();            Toast.makeText(getBaseContext(),选择了+国家[指数] +县,Toast.LENGTH_SHORT).show();            最终的CharSequence [] = state_array state.getTextArray(指数);            最后的微调state_spinner =(微调)findViewById(R.id.state);            ArrayAdapter<&CharSequence的GT;适配器1 =新ArrayAdapter<&CharSequence的GT;(MainActivity.this,android.R.layout.simple_spinner_item,state_array);            state_spinner.setAdapter(适配器1);            state_spinner.setOnItemSelectedListener(新OnItemSelectedListener(){                公共无效onItemSelected(适配器视图<>为arg0,ARG1观,诠释ARG2,长ARG3){                    最终诠释指数= arg0.getSelectedItemPosition();                    Toast.makeText(getBaseContext(),选择了+ state_array [指数] +国家,Toast.LENGTH_SHORT).show();        });                }                公共无效onNothingSelected(适配器视图<>为arg0){                    state_spinner.setAdapter(NULL);                }            }); 

I am new to Android Development so the query may seem novice.

I was trying to make an app where there are a couple of spinners and after the user selects one spinner the other spinner gets filled up with the data based on the selection in spinner1.

ie. First user selects Country. Based on country the spinner 2 gets filled up with states and based on the state the spinner 3 gets filled up with cities.

I have created all the countries as a string arrays in strings.xml like -

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

Similarly states have been created in the string.xml as - (Each Country is a separate entry with a naming convention as (country name)_(States)

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

Now I want the second spinner to populate based in country selection. Hence the formula used by me is to the get the second spinner is -

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

When I run this app the Manactivity just shows a blank screen and I donot get any Debug info and get an error "the jar file android.jar has no source attached".

Please help. The complete mainActivity is given below -

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;
}

解决方案

I was able to solve the problem. The final code is given below. This is a nested approach so if there are 3 spinners (like city after state) do we again nest the code for that spinner? Is there any simpler method?

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);
                }
            });