如何用android显示图像

时间:2014-06-03 10:03:56

标签: android

在我的活动中,我有两个旋转器。基于第一个旋转器,第二个旋转器必须改变。我明白了。但问题是当我在第二个微调器中选择项目时,不会显示相应的图像。

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter; 
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.Toast;

public class MainActivity extends Activity  implements    AdapterView.OnItemSelectedListener
{
ImageView im;
Object result,result1;
String[] country = { "Birds", "Countries", "Cars","Animals"};

String[] Birds_Birds = { "parrot", "peacock", "Sparrow","Pigeon"};
String[]items={"b1","b2","b3","b4"};
int[] imgIds={R.drawable.b1,R.drawable.b2,R.drawable.b3,R.drawable.b4};
String[] Countries_Countries = { "India", "Pakistan", "libya","Dubai"};
String[] Cars_Cars = { "Benz", "BMW", "Skoda","Ambassador"};
String[] Animals_Animals = { "Tiger", "leopard", "Horse","Rabbit"};

int fee,tot=0,atot=0;
Spinner spin0,spin1;


String Name;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //EditText display1=(EditText)findViewById(R.id.editText1);
    im=(ImageView)findViewById(R.id.imageView1);
    spin0 = (Spinner) findViewById(R.id.spinner1);
    spin0.setOnItemSelectedListener((OnItemSelectedListener) this);
    ArrayAdapter <String> c = new ArrayAdapter <String>      
  (this,android.R.layout.simple_spinner_item,country);
    c.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spin0.setAdapter(c);


    spin1 = (Spinner) findViewById(R.id.spinner2);
    spin1.setOnItemSelectedListener((OnItemSelectedListener) this);
    spin1.setEnabled(false);


   }
   @SuppressWarnings("null")
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {

    if(arg0.equals(spin0)){

            spin1.setEnabled(true);
                        if(spin0.getSelectedItem().equals("Birds"))
                        {
                            ArrayAdapter <String> s1 = new ArrayAdapter <String>    
     (this,android.R.layout.simple_spinner_item,Birds_Birds);
    s1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                            spin1.setAdapter(s1);
                            AdapterView<SpinnerAdapter> parent = null;
                            int pos = 0;

      parent.getItemAtPosition(pos);
                            switch (pos) {
                            case 0:
                                im.setImageResource(R.drawable.b1);
                                break;
                            case 1:
                                im.setImageResource(R.drawable.b2);         
                                break;
                            case 2:
                                im.setImageResource(R.drawable.b3);         
                                break;
                            default:
                                break;
                        }


                        }

                     result1=spin1.getSelectedItem();
                     result=spin0.getSelectedItem();

                   // display1.setText(""+result +"     /    "+result1); 



    }    
    public void onNothingSelected(AdapterView<?> arg0) {                

    }
   }

1 个答案:

答案 0 :(得分:1)

当你在spin1中选择一个项目时,没有做任何事情,因为你已经完成了

if(arg0.equals(spin0)){

,只有在选择sp0中的任何项目时才会出现这种情况。

如果您从sp1中选择一个项目,那么这将是错误的,因此不会发生任何事情。

如果您想对该项目执行任何操作,请选择sp1添加else部分