根据在两个微调器中选择的值打开不同的布局

时间:2017-07-20 06:32:43

标签: android android-layout

我是android的新手,在这段代码中我有两个微调器,根据选择的值,不同的布局应该相应打开。我坚持应该用什么逻辑来打开不同的布局,在spin的OnItemSelected里面。我在旋转器中有5个项目,每个项目有5个不同的布局。所以我该怎么办?

非常感谢任何帮助或想法。提前谢谢。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout);
    // Spinner element
    Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
    Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
    Button button = (Button) findViewById(R.id.button);
    // Spinner click listener

    // Spinner Drop down elements
    List<String> categories = new ArrayList<String>();
    categories.add("Maruti");
    categories.add("Ford");
    categories.add("Nissan");
    categories.add("TATA");

    List<String> types = new ArrayList<String>();
    types.add("1Lakh-5Lakh");
    types.add("5Lakh-10Lakh");
    types.add("10Lakh-20Lakh");
    types.add("20Lakh-50Lakh");
    types.add("Above 50Lakhs");

    // Creating adapter for spinner
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
    // Drop down layout style - list view with radio button
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    ArrayAdapter<String> spinner2Adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,types);
    spinner2Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // attaching data adapter to spinner
    spinner1.setAdapter(dataAdapter);
    spinner2.setAdapter(spinner2Adapter);

    spinner1.setOnItemSelectedListener(this);
    spinner2.setOnItemSelectedListener(this);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i=new Intent(SecondActivity.this,ThirdActivity.class);
            startActivity(i);
        }
    });
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    Spinner spinner1 = (Spinner)parent;
    Spinner spinner2 = (Spinner)parent;
    if(spinner1.equals(position))
    {

    }
    if(spinner2.getId() == R.id.spinner2)
    {

    }

}
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
}}

1 个答案:

答案 0 :(得分:0)

请尝试以下操作。 检查按钮的onClickListener,它将决定打开哪个活动。

Spinner spinner1;
Spinner spinner2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout);
    // Spinner element
     spinner1 = (Spinner) findViewById(R.id.spinner1);
     spinner2 = (Spinner) findViewById(R.id.spinner2);
    Button button = (Button) findViewById(R.id.button);
    // Spinner click listener
// Spinner Drop down elements
List<String> categories = new ArrayList<String>();
categories.add("Maruti");
categories.add("Ford");
categories.add("Nissan");
categories.add("TATA");

List<String> types = new ArrayList<String>();
types.add("1Lakh-5Lakh");
types.add("5Lakh-10Lakh");
types.add("10Lakh-20Lakh");
types.add("20Lakh-50Lakh");
types.add("Above 50Lakhs");

// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

ArrayAdapter<String> spinner2Adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,types);
spinner2Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

// attaching data adapter to spinner
spinner1.setAdapter(dataAdapter);
spinner2.setAdapter(spinner2Adapter);

spinner1.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent i;
        if(spinner1_selected_item.equals("Maruti"))
        {
            if(spinner2_selected_item.equals("1Lakh-5Lakh"))
                i=new Intent(SecondActivity.this,ThirdActivity.class);
            if(spinner2_selected_item.equals("5Lakh-10Lakh"))
                i=new Intent(SecondActivity.this,FourthActivity.class);    
            if(spinner2_selected_item.equals("10Lakh-20Lakh"))
                i=new Intent(SecondActivity.this,FifthActivity.class);        
        }

        startActivity(i);
    }
});
}

String spinner1_selected_item;
String spinner2_selected_item;
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    if(spinner1.getId() == R.id.spinner1)
    {
        spinner1_selected_item = parent.getItemAtPosition(position).toString();
    }
    if(spinner2.getId() == R.id.spinner2)
    {
        spinner2_selected_item = parent.getItemAtPosition(position).toString();
    }
}

public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
}}