【问题标题】:How can I display second spinner choice in Textview?如何在 Textview 中显示第二个微调器选项?
【发布时间】: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);
        }
    }

【问题讨论】:

    标签: android textview spinner


    【解决方案1】:

    试试这个方法,希望能帮助你解决问题。

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

    TextView kcalvaartus;
    ArrayAdapter<String> dataAdapter;
    

    在 onCreate() 中初始化 TextView:

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

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

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

    【讨论】:

    • 与其他答案相同的问题!现在 spinner2 不显示(实际上根本没有任何内容)微调器 1 选项。
    【解决方案2】:

    首先! 你在哪里设置了你的第一个 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
              }
        }
    

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

    【讨论】:

    • 似乎 spinner 2 没有使用此代码读取 spinner 1 的内容。 May the reason be, that when spinner 1 item is selected, spinner2 takes its content from txt file?
    • 在你从微调器 2 中选择一个项目并在 textview 中显示它之后,你想获得微调器 1 的选定项目吗?!
    • 是的!首先我选择 spinner1 - food category,然后选择 spinner 2。在 txt 文件中,有一种食品及其卡路里。我的最终解决方案应该是它在文本视图中显示卡路里,并在微调器 2 中显示产品名称。但我只是想让它至少像这样工作,这样我就可以继续开发它。所以从 spinner1 中选择项目(选择改变 spinner 2 的内容)然后是 spinner2。我在微调器 2 中选择的项目将出现在 textview 中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多