如何在Textview中显示第二个微调器?

时间:2014-11-05 10:00:15

标签: android textview spinner

我的第二个微调器内容(spFoodProduct)取决于第一个微调器选项(spFoodCategory)。这是完美的工作,但是一旦我添加另一个setOnItemSelectedListener(this)并尝试将第二个微调器中的值(并被单击)放到textview中,它就不起作用了!我需要的是textview根据我用第二个微调器做出的选择来改变它的值。 当我选择"牛奶" textview应该说" milk"。 我怎样才能使它工作?

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

2 个答案:

答案 0 :(得分:0)

请尝试这种方式,希望这有助于您解决问题。

在onItemSelected之外声明TextView和dataAdapter意味着在spinner声明的类级别:

TextView kcalvaartus;
ArrayAdapter<String> dataAdapter;

在onCreate()中初始化TextView:

kcalvaartus = (TextView) findViewById(R.id.kcalnumber1);

如果选择了spFoodCategory项,请检查选择了哪个微调器项目然后使用getProducts初始化dataAdapter()else set kcalvaartus set text usign选择spFoodProduct项目:

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    switch (view.getId()){
       case R.id.spinner1 :
            try{
                List<String> products = getProducts();
                dataAdapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item, products);
                dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spFoodProduct.setAdapter(dataAdapter);
            } catch (Exception e) {
                e.printStackTrace();
            }
            break;
       case R.id.spinner2 :
            kcalvaartus.setText(spFoodProduct.getSelectedItem().toString());
            break;
       default:
            break;
   }
}

答案 1 :(得分:0)

首先! 你在哪里设置了第一个Spinner的适配器?!?!?!

你应该做的是:

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {  
          if(arg1.getId==spFoodCategory.getId())
          {
             // Do something
             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);
          }
          if(arg1.getId==spFoodProduct.getId())
          {
          // Do something else
             TextView kcalvaartus = (TextView) findViewById(R.id.kcalnumber1);
             String str =  spFoodProduct.getAdapter().getItem(arg2).toString();
             kcalvaartus.setText(str); <-- Trying to setText to textview
          }
    }

我认为问题在于,当你点击该项目时,微调器会再次填充,因为你每次点击都会为它设置一个新的适配器。