【发布时间】:2014-11-07 00:23:08
【问题描述】:
我的第二个微调器内容 (spFoodProduct) 取决于第一个微调器选择 (spFoodCategory)。这工作得很好,但是一旦我添加另一个 setOnItemSelectedListener(this) 并尝试将第二个微调器中的值(并被单击)放到 textview 中,它就不起作用了!我需要的是 textview 根据我对第二个微调器所做的选择来更改其值。 当我选择“牛奶”时,文本视图应该说“牛奶”。 我怎样才能让它工作?
package com.kalorid.kalorikalkulaator;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class Categories extends Activity implements OnItemSelectedListener {
Spinner spFoodCategory, spFoodProduct;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_categories);
spFoodCategory = (Spinner) findViewById(R.id.spinner1);
spFoodProduct = (Spinner) findViewById(R.id.spinner2);
spFoodCategory.setOnItemSelectedListener(this);
spFoodProduct.setOnItemSelectedListener(this);
}
public List<String> getProducts() throws Exception {
String foodCategory = String.valueOf(spFoodCategory.getSelectedItem());
List<String> list = new ArrayList<String>();
InputStream txt = null;
if (foodCategory.equals("Piimatooted")) {
txt = getAssets().open("milkProducts");
} else if (foodCategory.equals("Liha- ja kalatooted")) {
txt = getAssets().open("meatAndFishProducts");
} else if (foodCategory.equals("Koogiviljad")) {
txt = getAssets().open("vegetableProducts");
} else if (foodCategory.equals("Teraviljatooted")) {
txt = getAssets().open("cerealProducts");
}
Toast.makeText(this, foodCategory, Toast.LENGTH_SHORT).show();
BufferedReader in = new BufferedReader(new InputStreamReader(txt, "UTF-8"));
String str;
while ((str=in.readLine()) != null) {
list.add(str);
}
in.close();
return list;
}
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
TextView kcalvaartus = (TextView) findViewById(R.id.kcalnumber1);
List<String> products = null;
try {
products = this.getProducts();
} catch (Exception e) {
e.printStackTrace();
}
//String str = spFoodProduct.getSelectedItem().toString();
//kcalvaartus.setText(str); <-- Trying to setText to textview
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, products);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
spFoodProduct.setAdapter(dataAdapter);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
【问题讨论】: