2013-02-09 54 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

相关问题