在 android 中将参数传递给微调器
Passing a parameter to spinner in android
我有 2 个微调器,首先从 json 响应中将值加载到第一个微调器。然后 select 来自第一个微调器的值然后来自第一个微调器的 selected 值将被发送到一个方法另一个方法称为 getFilteredDescriptions()
并且它 returns 一个 ArrayList() 称为 resultDescription
。我想将它设置为第二个微调器的输入。
我已将值 resultDescription
分配给第二个微调器,但我没有以正确的方式完成,因为我已经初始化或声明。谁能告诉我该怎么做?任何帮助将不胜感激。
@Override
public void onTaskCompleted(JSONArray responseJson) {
try {
List<String> crust = new ArrayList<String>();
final List<String> description = new ArrayList<String>();
for (int i = 0; i < responseJson.length(); ++i) {
JSONObject object = responseJson.getJSONObject(i);
if ((object.getString("MainCategoryID")).equals("1")) {
Log.i("Description ", object.getString("Description"));
descriptionHalf.add(object.getString("Description"));
JSONArray subMenuArray = object
.getJSONArray("SubMenuEntity");
for (int j = 0; j < subMenuArray.length(); ++j) {
JSONObject subMenuObject = subMenuArray
.getJSONObject(j);
Log.i("Crust", subMenuObject.getString("Crust"));
crust.add(subMenuObject.getString("Crust"));
Log.i("Description",
subMenuObject.getString("Description"));
description.add(subMenuObject.getString("Description"));
Log.i("Description",
subMenuObject.getString("Description"));
}
}
crustSP = (Spinner) findViewById(R.id.sp_crust);
sizeSP = (Spinner) findViewById(R.id.sp_pizza_size);
crust = Utils.removeDuplicatesFromList(crust);
ArrayAdapter<String> dataAdapterCru = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, crust);
dataAdapterCru
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
crustSP.setAdapter(dataAdapterCru);
crustSP.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onNothingSelected(AdapterView<?> arg0) {
}
@Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
String crustSelectedItem = crustSP.getSelectedItem()
.toString();
getFilteredDescriptions(crustSelectedItem, description);
}
});
ArrayAdapter<String> dataAdapterDes = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, resultDescription); //resultDescription cannot be resolved to a variable
dataAdapterDes
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sizeSP.setAdapter(dataAdapterDes);
sizeSP.setAdapter(new NothingSelectedSpinnerAdapter(
dataAdapterDes,
R.layout.contact_spinner_row_nothing_selected, this));
sizeSP.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onNothingSelected(AdapterView<?> arg0) {
}
@Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
List<String> getFilteredDescriptions(String crustSelectedItem,
List<String> description) {
List<String> resultDescription = new ArrayList<String>();
crustSelectedItem = crustSP.getSelectedItem().toString();
if (description == null || description.isEmpty())
return resultDescription;
for (int i = 0; i < description.size(); i++) {
description = Utils.removeDuplicatesFromList(description);
if (!description.get(i).contains(crustSelectedItem))
continue;
resultDescription.add(description.get(i));
}
return resultDescription; //send this above dataAdapterDes Adapter
}
您的代码如下所示。
@Override
public void onTaskCompleted(JSONArray responseJson) {
try {
List<String> crust = new ArrayList<String>();
final List<String> description = new ArrayList<String>();
for (int i = 0; i < responseJson.length(); ++i) {
JSONObject object = responseJson.getJSONObject(i);
if ((object.getString("MainCategoryID")).equals("1")) {
Log.i("Description ", object.getString("Description"));
descriptionHalf.add(object.getString("Description"));
JSONArray subMenuArray = object
.getJSONArray("SubMenuEntity");
for (int j = 0; j < subMenuArray.length(); ++j) {
JSONObject subMenuObject = subMenuArray
.getJSONObject(j);
Log.i("Crust", subMenuObject.getString("Crust"));
crust.add(subMenuObject.getString("Crust"));
Log.i("Description",
subMenuObject.getString("Description"));
description.add(subMenuObject.getString("Description"));
Log.i("Description",
subMenuObject.getString("Description"));
}
}
crustSP = (Spinner) findViewById(R.id.sp_crust);
sizeSP = (Spinner) findViewById(R.id.sp_pizza_size);
crust = Utils.removeDuplicatesFromList(crust);
ArrayAdapter<String> dataAdapterCru = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, crust);
dataAdapterCru
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
crustSP.setAdapter(dataAdapterCru);
crustSP.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onNothingSelected(AdapterView<?> arg0) {
}
@Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
String crustSelectedItem = crustSP.getSelectedItem()
.toString();
List<String> resultDescription = getFilteredDescriptions(crustSelectedItem, description);
ArrayAdapter<String> dataAdapterDes = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, resultDescription); //resultDescription cannot be resolved to a variable
dataAdapterDes
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sizeSP.setAdapter(dataAdapterDes);
sizeSP.setAdapter(new NothingSelectedSpinnerAdapter(
dataAdapterDes,
R.layout.contact_spinner_row_nothing_selected, this));
}
});
sizeSP.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onNothingSelected(AdapterView<?> arg0) {
}
@Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
还要学习一些基本的编码。而不是 this
使用 Activity/Class_Name.this
我有 2 个微调器,首先从 json 响应中将值加载到第一个微调器。然后 select 来自第一个微调器的值然后来自第一个微调器的 selected 值将被发送到一个方法另一个方法称为 getFilteredDescriptions()
并且它 returns 一个 ArrayList() 称为 resultDescription
。我想将它设置为第二个微调器的输入。
我已将值 resultDescription
分配给第二个微调器,但我没有以正确的方式完成,因为我已经初始化或声明。谁能告诉我该怎么做?任何帮助将不胜感激。
@Override
public void onTaskCompleted(JSONArray responseJson) {
try {
List<String> crust = new ArrayList<String>();
final List<String> description = new ArrayList<String>();
for (int i = 0; i < responseJson.length(); ++i) {
JSONObject object = responseJson.getJSONObject(i);
if ((object.getString("MainCategoryID")).equals("1")) {
Log.i("Description ", object.getString("Description"));
descriptionHalf.add(object.getString("Description"));
JSONArray subMenuArray = object
.getJSONArray("SubMenuEntity");
for (int j = 0; j < subMenuArray.length(); ++j) {
JSONObject subMenuObject = subMenuArray
.getJSONObject(j);
Log.i("Crust", subMenuObject.getString("Crust"));
crust.add(subMenuObject.getString("Crust"));
Log.i("Description",
subMenuObject.getString("Description"));
description.add(subMenuObject.getString("Description"));
Log.i("Description",
subMenuObject.getString("Description"));
}
}
crustSP = (Spinner) findViewById(R.id.sp_crust);
sizeSP = (Spinner) findViewById(R.id.sp_pizza_size);
crust = Utils.removeDuplicatesFromList(crust);
ArrayAdapter<String> dataAdapterCru = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, crust);
dataAdapterCru
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
crustSP.setAdapter(dataAdapterCru);
crustSP.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onNothingSelected(AdapterView<?> arg0) {
}
@Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
String crustSelectedItem = crustSP.getSelectedItem()
.toString();
getFilteredDescriptions(crustSelectedItem, description);
}
});
ArrayAdapter<String> dataAdapterDes = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, resultDescription); //resultDescription cannot be resolved to a variable
dataAdapterDes
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sizeSP.setAdapter(dataAdapterDes);
sizeSP.setAdapter(new NothingSelectedSpinnerAdapter(
dataAdapterDes,
R.layout.contact_spinner_row_nothing_selected, this));
sizeSP.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onNothingSelected(AdapterView<?> arg0) {
}
@Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
List<String> getFilteredDescriptions(String crustSelectedItem,
List<String> description) {
List<String> resultDescription = new ArrayList<String>();
crustSelectedItem = crustSP.getSelectedItem().toString();
if (description == null || description.isEmpty())
return resultDescription;
for (int i = 0; i < description.size(); i++) {
description = Utils.removeDuplicatesFromList(description);
if (!description.get(i).contains(crustSelectedItem))
continue;
resultDescription.add(description.get(i));
}
return resultDescription; //send this above dataAdapterDes Adapter
}
您的代码如下所示。
@Override
public void onTaskCompleted(JSONArray responseJson) {
try {
List<String> crust = new ArrayList<String>();
final List<String> description = new ArrayList<String>();
for (int i = 0; i < responseJson.length(); ++i) {
JSONObject object = responseJson.getJSONObject(i);
if ((object.getString("MainCategoryID")).equals("1")) {
Log.i("Description ", object.getString("Description"));
descriptionHalf.add(object.getString("Description"));
JSONArray subMenuArray = object
.getJSONArray("SubMenuEntity");
for (int j = 0; j < subMenuArray.length(); ++j) {
JSONObject subMenuObject = subMenuArray
.getJSONObject(j);
Log.i("Crust", subMenuObject.getString("Crust"));
crust.add(subMenuObject.getString("Crust"));
Log.i("Description",
subMenuObject.getString("Description"));
description.add(subMenuObject.getString("Description"));
Log.i("Description",
subMenuObject.getString("Description"));
}
}
crustSP = (Spinner) findViewById(R.id.sp_crust);
sizeSP = (Spinner) findViewById(R.id.sp_pizza_size);
crust = Utils.removeDuplicatesFromList(crust);
ArrayAdapter<String> dataAdapterCru = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, crust);
dataAdapterCru
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
crustSP.setAdapter(dataAdapterCru);
crustSP.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onNothingSelected(AdapterView<?> arg0) {
}
@Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
String crustSelectedItem = crustSP.getSelectedItem()
.toString();
List<String> resultDescription = getFilteredDescriptions(crustSelectedItem, description);
ArrayAdapter<String> dataAdapterDes = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, resultDescription); //resultDescription cannot be resolved to a variable
dataAdapterDes
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sizeSP.setAdapter(dataAdapterDes);
sizeSP.setAdapter(new NothingSelectedSpinnerAdapter(
dataAdapterDes,
R.layout.contact_spinner_row_nothing_selected, this));
}
});
sizeSP.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onNothingSelected(AdapterView<?> arg0) {
}
@Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
还要学习一些基本的编码。而不是 this
使用 Activity/Class_Name.this