0

I have a GridView with a BaseAdapter that shows 6 buttons like this:

  • Portrait:

B11 B12

B21 B22

B31 B32

  • Landscape:

B11 B12 B21

B22 B31 B32

Everything works great, until I switch from landscape back to portrait. It then shows:

  • Portrait:

B11 B12

B21 B22

B11 B32

So it shows B11 twice. When I click it, it still gets the correct position (4) and crashes because it can't find the correct button ID.

Here is the code for the adapter:

public class RightSlideMenuGridAdapter extends BaseAdapter {
private Context mContext;
private String TAG = "RightSlideMenuGridAdapter";

public RightSlideMenuGridAdapter(Context c) {
    mContext = c;
}

public int getCount() {
    return 6;
}

public Object getItem(int position) {
    Log.i(TAG, "getItem" + Integer.toString(position));
    return null;
}

public long getItemId(int position) {
    Log.i(TAG, "getItemId" + Integer.toString(position));
    long retId = 0;
    // Nothing worked here. Maybe I should return something?
    return retId;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    View child = convertView;
    if (child == null) {
        Context c = CobraApplication.getAppContext();
        Log.i(TAG, "getView" + Integer.toString(position));
        switch (position) {
        case 0:
            child = (View) ((LayoutInflater) CobraApplication
                    .getAppContext().getSystemService(
                            Context.LAYOUT_INFLATER_SERVICE)).inflate(
                    R.layout.toolscity, null);
            Button btnToolscityhwy = (Button) child
                    .findViewById(R.id.toolscityhwy);
            if (PersistentStoreHelper
                    .getDetectorSetting(DetectorSettings.DIR_MOB_IRAD_CITY_MODE
                            .name()) == CommunicationProtocol.DIR_MOB_IRAD_SETTING_HIGHWAY) { // VK:
                                                                                              // The
                                                                                              // setting
                                                                                              // is
                                                                                              // highway
                // VK: Show the highway icon
                btnToolscityhwy.setBackgroundDrawable(CobraApplication
                        .getAppContext().getResources()
                        .getDrawable(R.drawable.toolshwy));
                btnToolscityhwy.setText(c.getResources().getString(
                        R.string.city));
            } else {
                btnToolscityhwy.setBackgroundDrawable(CobraApplication
                        .getAppContext().getResources()
                        .getDrawable(R.drawable.toolscity));
                btnToolscityhwy.setText(c.getResources().getString(
                        R.string.highway));
            }
            break;

        case 1:
            child = (View) ((LayoutInflater) CobraApplication
                    .getAppContext().getSystemService(
                            Context.LAYOUT_INFLATER_SERVICE)).inflate(
                    R.layout.toolscarfinderbutton, null);
            ToggleButton btnToolsCarFinder = (ToggleButton) child
                    .findViewById(R.id.myToggleButtoncarfinder);
            // VK: Here we need to check the current setting of car finder
            // and set button state appropriately
            if (PersistentStoreHelper
                    .getNonDetectorSetting(NonDetectorSettings.CAR_FINDER_SETTING
                            .name()) == CommunicationProtocol.DIR_MOB_IRAD_SETTING_ON) {
                btnToolsCarFinder.setChecked(true);
            } else {
                btnToolsCarFinder.setChecked(false);
            }
            break;

        case 2:
            child = (View) ((LayoutInflater) CobraApplication
                    .getAppContext().getSystemService(
                            Context.LAYOUT_INFLATER_SERVICE)).inflate(
                    R.layout.toolstrafficbutton, null);
            ToggleButton btnToolsTraffic = (ToggleButton) child
                    .findViewById(R.id.myToggleButtonTraffic);
            btnToolsTraffic.setChecked(PersistentStoreHelper
                    .getTrafficDisplayState());
            break;

        case 3:
            child = (View) ((LayoutInflater) CobraApplication
                    .getAppContext().getSystemService(
                            Context.LAYOUT_INFLATER_SERVICE)).inflate(
                    R.layout.toolsmenulegend, null);
            break;

        case 4:
            child = (View) ((LayoutInflater) CobraApplication
                    .getAppContext().getSystemService(
                            Context.LAYOUT_INFLATER_SERVICE)).inflate(
                    R.layout.toolsmusic, null);
            ToggleButton btnToolsMusic = (ToggleButton) child
                    .findViewById(R.id.myToggleButtonMusicControl);
            btnToolsMusic.setChecked(PersistentStoreHelper
                    .getMusicControlDisplayState());
            break;

        case 5:
            child = (View) ((LayoutInflater) CobraApplication
                    .getAppContext().getSystemService(
                            Context.LAYOUT_INFLATER_SERVICE)).inflate(
                    R.layout.toolsdirectionsmenu, null);

            break;
        }
    }
    child.setPadding(0, 0, 0, 0);
    child.setFocusable(false);
    child.setClickable(false);

    return child;
}

And here are the traces for: GridInit portrait - OK. Then change orientation to landscape - still OK. Then back to portrait. And it gets View twice.

02-26 09:17:14.960: I/RightSlideMenuGridAdapter(12207): getView0
02-26 09:17:14.970: I/RightSlideMenuGridAdapter(12207): getView1
02-26 09:17:14.985: I/RightSlideMenuGridAdapter(12207): getView2
02-26 09:17:15.005: I/RightSlideMenuGridAdapter(12207): getView3
02-26 09:17:15.015: I/RightSlideMenuGridAdapter(12207): getView4
02-26 09:17:15.030: I/RightSlideMenuGridAdapter(12207): getView5
02-26 09:17:20.660: I/RightSlideMenuGridView(12207): MenuSize:600
02-26 09:17:20.665: I/RightSlideMenuGridView(12207): MenuSize:600
02-26 09:17:20.680: I/RightSlideMenuGridAdapter(12207): getView0
02-26 09:17:20.685: I/RightSlideMenuGridAdapter(12207): getView1
02-26 09:17:20.690: I/RightSlideMenuGridAdapter(12207): getView2
02-26 09:17:20.695: I/RightSlideMenuGridAdapter(12207): getView3
02-26 09:17:20.700: I/RightSlideMenuGridAdapter(12207): getView4
02-26 09:17:20.705: I/RightSlideMenuGridAdapter(12207): getView5
02-26 09:17:20.825: I/RightSlideMenuGridAdapter(12207): getView0
02-26 09:17:20.835: I/RightSlideMenuGridAdapter(12207): getItemId0
02-26 09:17:28.680: I/RightSlideMenuGridView(12207): MenuSize:375
02-26 09:17:28.680: I/RightSlideMenuGridView(12207): MenuSize:375
02-26 09:17:28.700: I/RightSlideMenuGridAdapter(12207): getView0
02-26 09:17:28.705: I/RightSlideMenuGridAdapter(12207): getView1
02-26 09:17:28.710: I/RightSlideMenuGridAdapter(12207): getView2
02-26 09:17:28.715: I/RightSlideMenuGridAdapter(12207): getView3
02-26 09:17:28.905: I/RightSlideMenuGridAdapter(12207): getView0
02-26 09:17:28.935: I/RightSlideMenuGridAdapter(12207): getItemId0
02-26 09:17:28.940: I/RightSlideMenuGridAdapter(12207): getView5
Radu
  • 2,076
  • 2
  • 20
  • 40
  • @LaiVung I am already using the Log class. I don't understand what you mean. – Radu Feb 26 '13 at 08:24
  • @LaiVung Look above, you have it. It calls getView with position 4 sometimes - in the correct cases. Other times it calls getView with position 0 and shows it in position 4... – Radu Feb 26 '13 at 08:37
  • @LaiVung The crash and exception are just a consequence of pressing Button 4, which calls onClick 4, which does not find the correct button id. As it is not on screen! That is not the issue, though!!! The issue is I have button 0 shown twice! In positions 0 and 4. Read closely again. – Radu Feb 26 '13 at 08:54
  • @LaiVung It's no problem! I appreciate you trying to help. Please understand again. If I don't press the button in the erroneous case, it shows no Exception. No crash occurs. Just a wrong button on the screen! Ok? That is what we are trying to fix here. – Radu Feb 26 '13 at 08:59

0 Answers0