android - 如何显示图像取决于2个微调器值?

时间:2012-11-13 17:33:22

标签: android android-intent imageview spinner

我有2个微调器,每个10个值, 现在我想要的是当用户选择2个值时,返回另一个名为“YourPath”的活动的特定图片

这是第一个活动的代码

private Spinner spinner1, spinner2;
private Button btnSubmit;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.locationlist);


    addListenerOnButton();
    addListenerOnSpinnerItemSelection();

}

public void addListenerOnSpinnerItemSelection() {
    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner2 = (Spinner) findViewById(R.id.spinner2);

}

public void addListenerOnButton() {

    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner2 = (Spinner) findViewById(R.id.spinner2);
    btnSubmit = (Button) findViewById(R.id.button1);

    btnSubmit.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

            AlertDialog alertDialog = new AlertDialog.Builder(
                    LocationList.this).create(); // Read Update
            alertDialog.setTitle("Confirm Message");
            alertDialog.setMessage("You are in "
                    + String.valueOf(spinner1.getSelectedItem())
                    + "\nAnd You are going to "
                    + String.valueOf(spinner2.getSelectedItem()));

            alertDialog.setButton("Yes",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int which) {

                            launchIntent();

                        }
                    });

            alertDialog.setButton2("No",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int which) {

                        }
                    });

            alertDialog.show();
        }

        private void launchIntent() {
            Intent it = new Intent(LocationList.this, YourPath.class);
            it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(it);
        }

    });

}

}

现在我需要一个数据库才能做到这一点!!!

你有什么想法我怎么能这样做!!!!

感谢

1 个答案:

答案 0 :(得分:0)

您根本不需要数据库。事实上,唯一的原因就是如果你想动态地改变用户可能最终“你的路径”的图像。

如果您可以在创建应用程序时提供所有图像,只需将它们全部放在/ Drawable文件夹中,然后您将在YourPath活动中访问所需的图像。

我会抓住这些值并将它们放在变量中以便像这样使用它们:

String value1 = spinner1.getSelectedItem().toString();
String value2 = spinner2.getSelectedItem().toString();

然后,我会将几个字符串值粘贴到您发送给YourPath活动的意图中。你将在launchIntent方法中这样做:

private void launchIntent() {
        Intent it = new Intent(LocationList.this, YourPath.class);
        it.putExtra("value1", value1);
        it.putExtra("value2", value2);
        startActivity(it);
    }

然后,在YourPath类中,抓住这些值:

Intent i = getIntent();
Bundle extras = i.getExtras();

String value1 = extras.getString("value1");
String value2 = extras.getString("value2");

然后,您将执行一些if-else(或最好是切换)语句来确定要显示的图像

if(value1.equals("somevalue") && value2.equals("othervalue")){
    int imageResource = R.drawable.mypic;

    ImageView imageView = (ImageView) findViewById(R.id.myImageView);
    Drawable image = getResources().getDrawable(imageResource);
    imageView.setImageDrawable(image);
}

然后将my_path布局文件添加到res / layout文件夹

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/myImageView"
    ></ImageView>
</LinearLayout>

您现在应该可以访问myPath活动中的图片视图,只是不要忘记设置内容视图:

setContentView(R.layout.my_path);