2012-11-13 48 views
0

我有2個紡紗,每組10個值, 現在我想要什麼,當用戶選擇2倍的值返回特定的畫面上被稱爲「YourPath」另一個活動android - 如何顯示圖像取決於2個紡紗廠的價值?

這裏是第一次活動

代碼
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); 
     } 

    }); 

} 

}

現在我需要一個數據庫來做到這一點!

你有什麼想法,我該怎麼做!

謝謝

+0

你需要微調位置進入的意圖,然後進行基於這些檢索值加載圖像的條件語句。 – mango

回答

0

根本不需要數據庫。事實上,你唯一的理由是,如果你想動態地改變用戶最終可能在「你的道路上」的圖像。

如果您可以在創建應用程序時提供所有圖像,只需將它們全部放在/ Drawable文件夾中,然後您將在YourPath Activity中訪問您想要的圖像。

我會抓住這些值,並把它們放在一個變量使用它們像這樣:

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 /佈局文件夾

<?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); 
+0

感謝您的答案,但在代碼的最後一節我應該把什麼,而不是「myPic」和「我的ImageView」,我應該如何我的XML看起來像對不起,我問了很多問題,但我是一個初學者 –

+0

我改變了我的代碼略高於因此,首先在/ res下創建一個名爲/ drawable的新文件夾。把所有的圖像放在這裏。然後,通過調用它們的代碼在代碼中訪問它們。例如說你把mypic.png變成drawable,你可以通過調用R.drawable.mypic來訪問它。我將添加上面這個活動所需的XML佈局。目前它只是一個圖像視圖,佔據了整個屏幕。別擔心,我們都是這樣開始的! – Jameo

+0

非常感謝你拯救了我的生命先生oooh我該怎麼做才能更多地感謝你!我真的很感激,如果你只是給了我你的電子郵件或任何東西,所以我可以在未來與你聯繫 –