0

这里添加了 2 个微调器。如果我选择特定的 item1 和 item2,则必须显示特定的图像。例如,如果选择了“Gujarat”和“Gandhinagr”,则必须显示图像 abc.png。请帮帮我。

    public class MainActivity extends Activity implements OnItemSelectedListener {

private String[] INDIAN_STATE = { "Gujarat", "Andra Pradesh",
        "Arunachal Pradesh", "Assam", "Bihar", "Chhattisgarh", "Goa",
        "Haryana", "Himachal Pradesh", "Jammu and Kashmir", "Jharkhand",
        "Karnataka", "Kerala", "Madya Pradesh", "Maharashtra", "Manipur",
        "Meghalaya", "Mizoram", "Nagaland", "Orissa", "Punjab",
        "Rajasthan", "Sikkim", "Tamil Nadu", "Tripura", "Uttaranchal",
        "Uttar Pradesh", "West Bengal" };

private String[] INDIAN_STATE_CAPITAL = { "Gandhinagar", "Hyderabad",
        "Itangar", "Dispur", "Patna", "Raipur", "Panaji", "Chandigarh",
        "Shimla", "Srinagar and Jammu", "Ranchi", "Bangalore",
        "Thiruvananthapuram", "Bhopal", "Mumbai", "Imphal", "Shillong",
        "Aizawi", "Kohima", "Bhubaneshwar", "Chandigarh", "Jaipur",
        "Gangtok", "Chennai", "Agartala", "Dehradun", "Lucknow", "Kolkata"       };

Spinner spinnerState, spinnerCapital;
TextView tvState, tvCapital;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    System.out.println(INDIAN_STATE.length);
    System.out.println(INDIAN_STATE_CAPITAL.length);

    tvState = (TextView) findViewById(R.id.mystate);
    tvCapital = (TextView) findViewById(R.id.mystatescapital);

    spinnerState = (Spinner) findViewById(R.id.spinnerstate);
    spinnerCapital = (Spinner) findViewById(R.id.spinnercapital);

    ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, INDIAN_STATE);
    adapter_state
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerState.setAdapter(adapter_state);

    ArrayAdapter<String> adapter_state_capital = new ArrayAdapter<String>(
            this, android.R.layout.simple_spinner_item,
            INDIAN_STATE_CAPITAL);
    adapter_state_capital
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerCapital.setAdapter(adapter_state_capital);

    spinnerState.setOnItemSelectedListener(this);
    spinnerCapital.setOnItemSelectedListener(this);
}

public void onItemSelected(AdapterView<?> parent, View view, int position,
        long id) {

    spinnerState.setSelection(position);
    spinnerCapital.setSelection(position);

    String myState = (String) spinnerState.getSelectedItem();
    String myStatesCapital = (String) spinnerCapital.getSelectedItem();

    tvState.setText("My State is " + myState);
    tvCapital.setText("My State is " + myState + ".And its Capital is "
            + myStatesCapital);

}

public void onNothingSelected(AdapterView<?> parent) {

}

}

4

1 回答 1

0

看看文档:http: //developer.android.com/guide/topics/ui/controls/spinner.html

我将假设每个州可以有多个首都 - 否则,您根本不需要首都旋转器。

首先,根据上述微调器文档,摆脱

spinnerState.setSelection(position);
spinnerCapital.setSelection(position);

String myState = (String) spinnerState.getSelectedItem();
String myStatesCapital = (String) spinnerCapital.getSelectedItem();

并使用

parent.getItemAtPosition(pos)

然后,添加对您将用于显示照片的方法的调用。这是一些伪代码

private Drawable getSelectedImage(String state, String capital){

    // Compare your selections here
    if(state.equals("Foo"){
        if(capital.equals("Bar"){
              //get and set appropriate image here
              int imageResource = R.drawable.foo_bar;
              return getResources().getDrawable(imageResource);
        }
    }
}

确保将您的图像文件添加到适当的可绘制文件夹中。

于 2013-04-23T20:26:41.367 回答