我的所有微调器都是相互连接的,它们不是独立的,我想根据spinner1中的选择显示spinner2和spinner3中的选项(我的意思是基于用户在spinner1中选择的类别)
在onCreate
我将数据填充到Spinners中,但是在spinner2和spinner3中我得到的数据属于CategoryB,而它们只能填充CategoryA数据。
那我的错误在哪里?这是我的JSON解析代码:
categoryArrayList = new ArrayList<Category>();
cArrayList = new ArrayList<String>();
...................................
// Array Level 1 --- START
JSONArray jarray = jsono.getJSONArray("categories");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
Category language = new Category();
language.setName(object.getString("category_name"));
Log.d("category_name::-", object.getString("category_name"));
language.setTypeArrayList(typeArrayList);
categoryArrayList.add(language);
cArrayList.add(categoryArrayList.get(i).getName());
// Array Level 1 --- END
// Array Level 2 --- START
JSONArray jsarray = object.getJSONArray("types");
typeArrayList = new ArrayList<Type>();
tArrayList = new ArrayList<String>();
for (int j = 0; j < jsarray.length(); j++) {
JSONObject jjobject = jsarray.getJSONObject(j);
Type genre = new Type();
genre.setName(jjobject.getString("type_name"));
Log.d("type_name::-", jjobject.getString("type_name"));
genre.setServiceArrayList(serviceArrayList);
typeArrayList.add(genre);
tArrayList.add(typeArrayList.get(j).getName());
// Array Level 2 --- END
// Array Level 3 --- START
JSONArray jsonarray = jjobject.getJSONArray("services");
serviceArrayList = new ArrayList<Service>();
sArrayList = new ArrayList<String>();
for (int k = 0; k < jsonarray.length(); k++) {
JSONObject jjjobject = jsonarray.getJSONObject(k);
Service movie = new Service();
movie.setName(jjjobject.getString("service_name"));
Log.d("service_name::-", jjjobject.getString("service_name"));
serviceArrayList.add(movie);
sArrayList.add(serviceArrayList.get(k).getName());
// Array Level 3 --- END
}
}
}
return true;
}
以下是我如何填充Spinners:
spinner1.setAdapter(new ArrayAdapter<String>(CategoryActivity.this,
android.R.layout.simple_spinner_dropdown_item,
cArrayList));
spinner2.setAdapter(new ArrayAdapter<String>(CategoryActivity.this,
android.R.layout.simple_spinner_dropdown_item,
tArrayList));
spinner3.setAdapter(new ArrayAdapter<String>(CategoryActivity.this,
android.R.layout.simple_spinner_dropdown_item,
sArrayList));
答案 0 :(得分:5)
奥利奥。这是完整的代码。
类别类
public class Category {
String name;
ArrayList<ArrayList<Type>> type;
Category()
{
name = "";
type = new ArrayList<ArrayList<Type>>();
}
void setName(String s)
{
name =s;
}
public String getName()
{
return name;
}
void setTypeArrayList(ArrayList<Type> serviceArrayList)
{
type.add(serviceArrayList);
}
ArrayList<Type> getTypeArrayList(int i)
{
return type.get(i);
}
}
输入课程
import java.util.ArrayList;
public class Type {
String type;
ArrayList<ArrayList<Service>> service;
public Type() {
// TODO Auto-generated constructor stub
type = "";
service = new ArrayList<ArrayList<Service>>();
}
void setName(String s)
{
type =s;
}
public String getName()
{
return type;
}
void setServiceArrayList(ArrayList<Service> serviceArrayList)
{
service.add(serviceArrayList);
}
ArrayList<Service> getServiceArrayList(int i)
{
return service.get(i);
}
}
服务类
public class Service {
String service;
public Service() {
// TODO Auto-generated constructor stub
service ="";
}
void setName(String s)
{
service =s;
}
public String getName()
{
return service;
}
}
初始化全局
int catpos =0;
ArrayList<Category> categoryArrayList = new ArrayList<Category>();
ArrayList<String> cArrayList = new ArrayList<String>();
ArrayList<Type> typeArrayList;
ArrayList<Service> serviceArrayList;
doInbackground
//your http request and response process
JSONObject jsono;
try {
jsono = new JSONObject(data);
JSONArray jarray = jsono.getJSONArray("categories");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
Category language = new Category();
language.setName(object.getString("category_name"));
Log.d("category_name::-", object.getString("category_name"));
JSONArray jsarray = object.getJSONArray("types");
typeArrayList = new ArrayList<Type>();
tArrayList = new ArrayList<String>();
for (int j = 0; j < jsarray.length(); j++) {
JSONObject jjobject = jsarray.getJSONObject(j);
Type genre = new Type();
genre.setName(jjobject.getString("type_name"));
Log.d("type_name::-", jjobject.getString("type_name"));
JSONArray jsonarray = jjobject.getJSONArray("services");
serviceArrayList = new ArrayList<Service>();
sArrayList = new ArrayList<String>();
for (int k = 0; k < jsonarray.length(); k++) {
JSONObject jjjobject = jsonarray.getJSONObject(k);
Service movie = new Service();
movie.setName(jjjobject.getString("service_name"));
Log.d("service_name::-",
jjjobject.getString("service_name"));
serviceArrayList.add(movie);
sArrayList.add(serviceArrayList.get(k).getName());
}
genre.setServiceArrayList(serviceArrayList);
typeArrayList.add(genre);
tArrayList.add(typeArrayList.get(j).getName());
}
language.setTypeArrayList(typeArrayList);
categoryArrayList.add(language);
cArrayList.add(categoryArrayList.get(i).getName());
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
onPostExecute方法
tArrayList = new ArrayList<String>();
sArrayList = new ArrayList<String>();
Category c = categoryArrayList.get(0);
typeArrayList = c.getTypeArrayList(0);
for(int i=0;i<typeArrayList.size();i++)
tArrayList.add(typeArrayList.get(i).getName());
Type t = typeArrayList.get(0);
serviceArrayList = t.getServiceArrayList(0);
for(int i=0;i<serviceArrayList.size();i++)
sArrayList.add(serviceArrayList.get(i).getName());
spinner1.setAdapter(new ArrayAdapter<String>(CategoryActivity.this,
android.R.layout.simple_spinner_dropdown_item, cArrayList));
spinner2.setAdapter(new ArrayAdapter<String>(CategoryActivity.this,
android.R.layout.simple_spinner_dropdown_item, tArrayList));
spinner3.setAdapter(new ArrayAdapter<String>(CategoryActivity.this,
android.R.layout.simple_spinner_dropdown_item, sArrayList));
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
catpos = pos;
tArrayList = new ArrayList<String>();
sArrayList = new ArrayList<String>();
Category c = categoryArrayList.get(pos);
typeArrayList = c.getTypeArrayList(0);
for(int i=0;i<typeArrayList.size();i++)
tArrayList.add(typeArrayList.get(i).getName());
Type t = typeArrayList.get(0);
serviceArrayList = t.getServiceArrayList(0);
for(int i=0;i<serviceArrayList.size();i++)
sArrayList.add(serviceArrayList.get(i).getName());
spinner2.setAdapter(new ArrayAdapter<String>(CategoryActivity.this,
android.R.layout.simple_spinner_dropdown_item, tArrayList));
spinner3.setAdapter(new ArrayAdapter<String>(CategoryActivity.this,
android.R.layout.simple_spinner_dropdown_item, sArrayList));
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
sArrayList = new ArrayList<String>();
Category c = categoryArrayList.get(catpos);
typeArrayList = c.getTypeArrayList(0);
Type t = typeArrayList.get(pos);
serviceArrayList = t.getServiceArrayList(0);
for(int i=0;i<serviceArrayList.size();i++)
sArrayList.add(serviceArrayList.get(i).getName());
spinner3.setAdapter(new ArrayAdapter<String>(CategoryActivity.this,
android.R.layout.simple_spinner_dropdown_item, sArrayList));
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
答案 1 :(得分:2)
我的策略是:
1)首先,填充微调器1 并根据所选项目(第一项)填充微调器2,3 旋转器1。
2)放置spinner1.setOnItemSelectedListener()
并在此方法内的 spinner 1 上更改项目时发送事件。要发送活动,请使用Otto。
3)在Otto
,Activity
和Fragment
上注册该事件@Subscribe
。因此,当您在微调器1 中选择项目时,您将立即在@Subscribe
内收到通知,并且您必须使用spinner1.setAdapter()
更改微调器2,3 并且spinner2.setAdapter()
在那里。
答案 2 :(得分:2)
您tArrayList
的填充错误。您可以使用以下代码填充您的Spinner:
spinner2.setAdapter(new ArrayAdapter<String>(CategoryActivity.this,
android.R.layout.simple_spinner_dropdown_item,
tArrayList));
但是变量tArrayList只获取它读取的最后一个值。
您正在使用for循环,但在每个循环上重新启动tArrayList
变量,因此最后它只有最后一个值。
[...]
for (int i = 0; i < jarray.length(); i++) {
[...]
tArrayList = new ArrayList<String>();
其中一个解决方案可能是这样的:
List<List<String>> tArrayListOfLists = new ArrayList<ArrayList<>>();
for(...){ // First for loop
List<String> tArrayList = new ArrayList<>();
for(...) { // Second for loop
tArrayList.add("your strings");
}
tArrayListOfLists.add(tArrayList);
}
然后,您将获得包含所需项目的列表列表。要在微调器上列出用户选择并填充它:
spinner1.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
// Get the selected position
List<String> selectedList = tArrayListOfLists.get(position);
spinner2.setAdapter(new ArrayAdapter<String>(CategoryActivity.this,
android.R.layout.simple_spinner_dropdown_item, selectedList));
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// Clear the list 2
}
});
答案 3 :(得分:2)
取自this site:
此示例显示如何根据第一个微调器的选择填充第二个微调器。
XML:布局文件条目
<强> RES /布局/ activity_spinner_ex4.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" > <!-- Label --> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="8dip" android:text="@string/lblAcc" /> <!-- Spinner Dropdown --> <Spinner android:id="@+id/spinner1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="8dip" android:layout_marginRight="8dip" android:layout_marginTop="10dip" android:entries="@array/acc_type" /> <!-- Select Label --> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="8dip" android:text="@string/lblSubAcc" /> <!-- Spinner Dropdown --> <Spinner android:id="@+id/spinner2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:layout_marginLeft="8dip" android:layout_marginRight="8dip" /> </LinearLayout>
XML:字符串文件条目
<强> RES /值/ strings.xml中强>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Spinner Example</string> <string name="action_settings">Settings</string> <string name="lblAcc">Select Account Type</string> <string name="lblSubAcc">Select Account Head</string> <string-array name="acc_type"> <item>Income</item> <item>Expense</item> </string-array> </resources>
主要活动
<强> SpinnerEx4Activity.java 强>
package com.bar.example.androidspinnerexample; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.Toast; public class SpinnerEx4Activity extends Activity implements OnItemSelectedListener{ Spinner s1,s2; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_spinner_ex4); s1 = (Spinner)findViewById(R.id.spinner1); s2 = (Spinner)findViewById(R.id.spinner2); s1.setOnItemSelectedListener(this); } @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub String sp1= String.valueOf(s1.getSelectedItem()); Toast.makeText(this, sp1, Toast.LENGTH_SHORT).show(); if(sp1.contentEquals("Income")) { List<String> list = new ArrayList<String>(); list.add("Salary"); list.add("Sales"); list.add("Others"); ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list); dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); dataAdapter.notifyDataSetChanged(); s2.setAdapter(dataAdapter); } if(sp1.contentEquals("Expense")) { List<String> list = new ArrayList<String>(); list.add("Conveyance"); list.add("Breakfast"); list.add("Purchase"); ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list); dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); dataAdapter2.notifyDataSetChanged(); s2.setAdapter(dataAdapter2); } } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }