0

I have a program that shows a list of floors, and when one of the floors in the list is selected the program opens another activity showing an image of the selected floor. The problem is that the second activity only shows one floor no matter what floor you select.

This is my 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);
            }

        });

    }




}

Here is the other activity:

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);
}

}

Do I have to use a different method to create a list?

2
  • What do your logs look like?
    – JstnPwll
    Apr 18, 2014 at 15:00
  • I tested your code, it works showing different images. Make sure you really have those images in res/drawable and clean the project.
    – Onik
    Apr 18, 2014 at 20:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.