2013-02-09 51 views
0

Spinner中選擇項目後如何顯示不同的內容? 我想創建一個Spinner與連鎖店的位置。同一活動中每個Spinner項目的不同內容

+0

究竟意味着什麼*即使選擇了地址後,我仍然希望微調器可見並顯示所有位置。 – Luksprog 2013-02-09 15:29:08

+0

我希望微調控制器始終處於最佳狀態。唯一更改爲微調器下的內容 – 2013-02-09 15:37:19

+0

spinner的1項= 1地址 – 2013-02-09 15:52:47

回答

0

我希望微調器總是在那裏。這 變化是微調

下的內容唯一創建活動爲您提供簡單的方法來刷新Spinner下方的佈局(這將保持不變)。該方法將從Spinner上設置的OnItemSelectedListener中調用。這將是這樣的:

private void changeAdress(int newSelectedAdress) { 
    // The ImageView and the TextView will be already in the layout 
    ImageView map = (ImageView) findViewById(R.id.theIdOfTheImage); 
    // Set the image. You know the current address selected by the user 
    // (the newSelectedAddress int) so get it from the array/list/database 
    // where you stored it 
    // also set the image 
} 

onItemSelected回調調用上述方法:

yourSpinnerRefference.setOnItemSelectedListener(new OnItemSelectedListener() { 

     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, 
       int position, long id) { 
          changeAddress(position);    
     } 

     @Override 
     public void onNothingSelected(AdapterView<?> parent) { 

     } 
    }); 

如果這不是你想要的,請解釋。

編輯:讓兩個數組來保存你的數據:

int[] images = {R.drawable.imag1, R.drawable.imag2 ..etc...}; 
//also for the text 
String[] text = {"text1", "text2 ...etc...}; 

然後在我前面推薦的方法使用這兩個數組:

private void changeAdress(int newSelectedAddress) { 
    ((ImageView)findViewById(R.id.mapView1)).setImageResource(images[newSelectedAddress]); 
    // assing an id to the TextView in your layout and do the same as above. 
} 

沒有必要多ImageViewTextViews

+0

我在編程時是noob,所以我在查看代碼時嘗試瞭解如何實現我想要的方式。 – 2013-02-09 16:50:10

+0

onItemSelected {if int = 0 - > Show Image1,Text1;如果int = 1 - > Show Image2; Text2 ... all在同一活動中。這就是我想要的方式 – 2013-02-09 16:51:25

+0

@CatalinH在'changeAddress'方法中,您會看到'newSelectedAdress'參數。如果它是'1',則加載第一個地址,如果加載了第二個地址,則加載第二個地址等等。我不知道如何存儲這些地址,例如,如果將它們放在數組中,那麼您只需獲得item來自該數組的索引'newSelectedAdress'。我已經編輯了一些答案。 – Luksprog 2013-02-09 16:57:21

相關問題