gpt4 book ai didi

android - (动态)多个微调器州/市

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:25:36 27 4
gpt4 key购买 nike

我必须开发一个 android View ,以便其中有 2 个微调器控件,一个用于州,第二个用于城市。

我的问题是,如何在选择状态时自动填充城市微调器?

背后的逻辑是什么?

我的 string.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="spinner_category"></string>
<string-array name="category_state">
<item >kerala</item>
<item >tamil nadu</item>
<item >Andra Pradesh</item>
<item >karnataka</item>
</string-array>
</resources>

我的 main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip"
android:text="Select : "/>
<Spinner android:layout_width="250dip"
android:layout_height="wrap_content"
android:id="@+id/spinner_state"/>
</LinearLayout>

<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip"
android:text="Select : "/>
<Spinner android:layout_width="250dip"
android:layout_height="wrap_content"
android:id="@+id/spinner_state"/>
</LinearLayout>
</LinearLayout>

还有我的 activity.java 文件:

package com.converter;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class ConverterActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Spinner spinner_s = (Spinner)findViewById(R.id.spinner_state);
ArrayAdapter<CharSequence> category_adapter = ArrayAdapter.createFromResource(
this, R.array.category_array, android.R.layout.simple_spinner_item);
category_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_s.setAdapter(category_adapter);

}
}

最佳答案

例子:

    Spinner city=(Spinner)findViewById(R.id.citySpinner);
Spinner state=(Spinner)findViewById(R.id.stateSpinner);

final ArrayList<String> state_options=new ArrayList<String>();
final ArrayList<String> city_options=new ArrayList<String>();

state_options.add("state_1");
state_options.add("state_2");
state_options.add("state_3");
// Here you can also get a cursor and add Strings as options to state_options instead of what i have done

city_options.add("city_1_state_1");
city_options.add("city_2_state_1");
city_options.add("city_3_state_1");
// Here you can also get a cursor and add Strings as options to city_options instead of what i have done

ArrayAdapter<String> cityAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item,city_options);
city.setAdapter(cityAdapter);

ArrayAdapter<String> stateAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item,state_options);
state.setAdapter(stateAdapter);

state.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> arg0, View view,
int position, long id) {

String stateName=state_options.get(position).toString();
resetCity(stateName);
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {

}
});

现在,

public void resetCity(String stateName)
{
city_options.removeAll(city_options);//i haven't checked this.
if(stateName.eqauls("state_1"))
{
city_option.add("city_1_state_1");
city_options.add("city_2_state_1");
city_options.add("city_3_state_1");
//you can also get a cursor and add Strings as options to city_options instead of what i have done
}
else if(stateName.eqauls("state_2"))
{
city_option.add("city_1_state_2");
city_options.add("city_2_state_2");
city_options.add("city_3_state_2");
// you can also get a cursor and add Strings as options to city_options instead of what i have done
}
else
{
city_option.add("city_1_state_3");
city_options.add("city_2_state_3");
city_options.add("city_3_state_3");
//you can also get a cursor and add Strings as options to city_options instead of what i have done
}

ArrayAdapter<String> cityAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item,city_options);
city.setAdapter(cityAdapter);
}

这是最简单的示例。您可以从数据库中设置您的 city_optionsstate_options。然后您可以使用它来填充相应的微调器。

关于android - (动态)多个微调器州/市,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8078654/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com