2016-02-23 29 views
1

所以,我加入了新的功能,我的尋寶應用程序,它基本上是顯示的線索圖片的能力。通過點擊線索的文字,我去一個ImageView的活動顯示圖片依賴於線索。問題是,我不知道如何使這項工作。我試着做一個地圖/ HashMap的組合....一個簡單的圖像彈出式

Map<String, Integer> map = new HashMap<String, Integer>(); 
map.put("info", R.drawable.clue0); 
map.put("goebel", R.drawable.clue1); 
map.put("church", R.drawable.clue2); 
map.put("confed", R.drawable.clue3); 
map.put("ban", R.drawable.clue4); 
map.put("jr", R.drawable.clue5); 
map.put("ohara", R.drawable.clue6); 
map.put("db", R.drawable.clue7); 

,然後填充與圖片...

imgClue.setImageResource(map.get("goebel")); 

點擊文本線索有這樣的代碼:

txtClue.setOnClickListener(new OnClickListener(){ 

      @Override 
      public void onClick(View v) { 
       Intent cluePic = new Intent(FCRun.this, imgClue.class); 
       Intent imgClue; 
       cluePic.putExtras(imgClue); 
      } 

     }); 

ImageView活動代碼(到目前爲止)是...

public class imgClue extends Activity{ 
public static ImageView iClue; 

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

} 

public static void setImageResource(Integer integer) { 

    }} 

我接近警報傳遞的ID來表示所顯示的位置。我使用Switch(ID)/ Case來設置根據位置在屏幕上顯示的文字。我希望我可以在Case語句中添加另一行來指出要顯示的圖片。

所以我的問題是,我要對這個錯誤的方式或者是我的代碼只是完全錯了嗎?

的代碼將無法編譯,因爲它只是不喜歡我的MAP報表週期。

感謝, Hendo

回答

1

,因爲這兩行的代碼不能編譯:

Intent imgClue; 
cluePic.putExtras(imgClue); 

刪除它們,並與

startActivity(cluePic); 

取代它們,這也是錯誤imgClue.setImageResource(map.get("goebel"));你不應該從地圖設置結果(即In teger drawable資源)`作爲一種靜態方法。你應該將其穿過的意圖,例如:

cluePic.putExtra( 「MyImageKey」,map.get( 「格貝爾」));

然後在你的活動(imgClue)內onCreate你會做

int imageDrawableResource = getIntent().getIntExtra("MyImageKey", 0); 

雖然你的代碼是不完美的,它是一個完全沒有辦法做到這一點。:-)


爲了使你的代碼更容易理解自己稍後我會建議這些變化:

  • 變化Map<String, Integer> mapMap<String, Integer> cluePictures
  • 變化txtClueclueTextView
  • 變化Intent cluePicIntent cluePictureIntent
  • 更改imgClue extends ActivityImageClueActivity extends Activity
  • public static ImageView iClue刪除靜態關鍵字和使私人:private ImageView clueImageView
在此行
+0

'cluePic.putExtra( 「MyImageKey」,map.get( 「格貝爾」));'是不是「goebel」圖像的關鍵?我想我不明白你指的是什麼「MyImageKey」。 –

+0

非常感謝您的幫助和回覆。 :-) –

+0

@DaveHenderson這行有兩個鍵。你是對的''「goebel」是你地圖的關鍵。然後,您將地圖中的值放入「Intent」中,並使用另一個鍵將其發送到下一個活動:-) – Blundell