我有一个显示楼层列表的程序,当选择列表中的一个楼层时,程序会打开另一个显示所选楼层图像的活动。问题是第二个活动只显示一个楼层,无论你选择什么楼层。
这是我的MainActivity:
public class MainActivity extends ListActivity{
ArrayList<String> floorArray = new ArrayList<String>();
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, floorArray);
this.setListAdapter(adapter);
floorArray.add("Våning 1");
floorArray.add("Våning 2");
floorArray.add("Våning 3");
floorArray.add("Våning 4");
floorArray.add("Våning 5");
floorArray.add("Våning 6");
floorArray.add("Våning 7");
adapter.notifyDataSetChanged();
this.getListView().setOnItemClickListener(new OnItemClickListener() {
private void onItemSelected(){
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Log.d("gym", "floor: " + floorArray.get(arg2));
Intent intent = new Intent(arg1.getContext(), DisplayFloorActivity.class);
intent.putExtra("floor",arg2);
startActivity(intent);
}
});
}
}
这是另一项活动:
public class DisplayFloorActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_floor);
// Show the Up button in the action bar.
setupActionBar();
showImage();
}
/**
* Set up the {@link android.app.ActionBar}.
*/
private void setupActionBar() {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
private void showImage(){
ImageView iView = (ImageView)findViewById(R.id.imageView1);
Bundle extras = this.getIntent().getExtras();
int arg2 = extras.getInt("floor");
if(arg2==0){
iView.setImageResource(R.drawable.floor1);
}
if(arg2==1){
iView.setImageResource(R.drawable.floor2);
}
if(arg2==2){
iView.setImageResource(R.drawable.floor3);
}
if(arg2==3){
iView.setImageResource(R.drawable.floor4);
}
if(arg2==4){
iView.setImageResource(R.drawable.floor5);
}
if(arg2==5){
iView.setImageResource(R.drawable.floor6);
}
if(arg2==6){
iView.setImageResource(R.drawable.floor7);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_floor, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
}
return super.onOptionsItemSelected(item);
}
}
我是否必须使用其他方法创建列表?