2015-10-05 18 views
0

我正在做一個小項目作爲Android課程的一部分。 應用程序應該使用某個網站的API檢索最流行的電影,然後將其顯示在網格視圖中(使用海報作爲縮略圖)。 點擊特定影片後,將開始一項新活動,提供詳情。Android - 設計麻煩 - 我應該使用setTag來保存OnClick事件的信息嗎?

我這個地步得到:

  1. 獲得最受歡迎的電影的海報圖片的URL使用本網站的API列表。
  2. 實現了一個擴展ArrayAdapter的適配器,它接受ArrayList作爲數據源並將URL中的圖像加載到ImageView項目中。
  3. 使用適配器填充GridView。

S::使用此代碼上的GridView

  • 設置聽衆

    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
          public void onItemClick(AdapterView<?> parent, View v, 
                int position, long id) { 
        //do stuff according to position/view 
    } 
    

    我需要通過到電影細節活性當影片被點擊可以在步驟1中獲得的信息(I剛從json中提取海報URL)。 爲了甚至能夠在點擊視圖時提供信息,我需要(至少)提取電影的ID並存儲它。

    所以,因爲它似乎我的選擇是:

    1. 商店中相應的適配器的ID。然後,當發生點擊時,使用getItem(position)來獲取ID並將其與意圖一起發送。接下來的活動將不得不向服務器查詢電影細節。

    這意味着創建一個類:

    static class MovieItem { 
        int Id; 
        string posterUrl; 
    } 
    

    並轉換適配器使用ArrayList<MovieItem>

    1. 與選項1相同,但改爲使用setTag來存儲電影的ID。
    2. 與選項1相同,取而代之的是獲得所有必需的信息(標題,評分,繪圖等)並將其存儲到MovieItem中。在下一個活動中不需要查詢。
    3. 與選項3相同,但改爲使用setTag(MovieItem)。在下一個活動中不需要查詢。

    我是新來的應用程序開發人員,我試圖以正確的方式完成任務,希望能得到一些幫助。

    編輯: 也想添加,如果我已經在適配器中存儲額外的電影信息將不適合,因爲信息是不相關的該類?

    感謝您的麻煩! :)

  • 回答

    0

    當您第一次請求電影信息列表時,您可以存儲每個電影的詳細信息,如HashMap<String, HashMap<String, String>>,其中String是電影ID,Map是一組鍵/值對詳細信息。然後,當onClick發生點擊時,您可以使用該位置來確定哪個電影海報被點擊。然後,您將檢索所選電影的詳細HashMap,將其放入Bundle中,將其傳遞給新Activity的Intent,然後在另一側檢索它。因此,代碼會是這個樣子:

    當你第一次檢索您的電影列表,你會做這樣的事情:

    //You would put each set of movie data into a HashMap like this... 
    HashMap<String, HashMap<String, String>> movies = new HashMap<>(); 
    
    HashMap<String, String> details = new HashMap<>(); 
    details.put("dateReleased", "7.12.2015"); 
    details.put("rating", "PG-13"); 
    details.put("title", "Cool Awesome Movie"); 
    movies.put("12345", details); 
    
    //Then when an onClick comes in, you would get the set of 
    //details for the movie that was selected (based on position) 
    HashMap<String, String> selectedDetails = movies.get("12345"); 
    
    //Put the retrieved HashMap of details into a Bundle 
    Bundle bundle = new Bundle(); 
    bundle.putSerializable("movieDetails", selectedDetails); 
    
    //Send the bundle over to the new Activity with the intent 
    Intent intent = new Intent(this, YourDetailsActivity.class); 
    intent.putExtras(bundle); 
    startActivity(intent); 
    

    那麼新活動開始時,您將檢索從細節捆綁在您的onCreate()中:

    //Then in onCreate() of the new Activity... 
    Bundle bundle = getIntent().getExtras(); 
    HashMap<String, String> movieDetails = (HashMap<String, String>) bundle.getSerializable("movieDetails"); 
    String dateReleased = movieDetails.get("dateReleased"); 
    String rating = movieDetails.get("rating"); 
    String title = movieDetails.get("title"); 
    
    +0

    感謝您的回答!這似乎是一個很好的選擇,但我想知道我是否使用過setTag會被濫用嗎? – Idh

    +0

    也想添加,如果我已經在適配器中存儲了額外的電影信息,那會不合適,因爲信息與該類無關? 對不起,如果我是一個麻煩,但我正在尋找答案,而不是'做這個'。謝謝:) – Idh

    +0

    當你說你想使用setTag()時,我假設你的意思是ImageView的setTag()?你當然可以用它來存儲這個id,然後按照你提到的意圖傳遞它。但!這也意味着你必須做兩個url請求,而不是一個。如果您的數據量很少(比如10張電影的列表),我可能會選擇一次下載所有數據,然後使用存儲的數據創建詳細信息活動。如果您喜歡100部電影,則可能會更好地獲取每次點擊的詳細信息。 – NoChinDeluxe