同一个Activity中每个Spinner项的不同内容

时间:2013-02-09 15:21:00

标签: android android-activity spinner

Spinner中选择项目后,如何显示不同的内容? 我想创建一个带有连锁店位置的Spinner

1 个答案:

答案 0 :(得分:0)

  

我希望微调器始终位于顶部。唯一的事情   更改为微调器下的内容

在您的活动中创建一个简单方法,以刷新Spinner下面的布局(这将保持不变)。该方法将从OnItemSelectedListener上的Spinner集调用。它会是这样的:

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

相关问题