views:

58

answers:

0

This is a clearer and more precise definition (with test code) of the problem originally detailed here http://stackoverflow.com/questions/3814392/app-working-under-2-1-android-now-hangs-on-2-2-both-in-emulator-and-desire.

I have narrowed the cause of the problem down to a call to notifyDataSetChanged(). The code works fine in versions prior to 2.2, but 'hangs' in a predictable (but wierd) way in 2.2.

Basically there are 2 buttons placed in a gridview. Pressing either of the buttons swaps them around. In versions prior to 2.2 this code works fine. However in 2.2 the code behaves strangely in that the first two clicks on buttons swap as expected and then the third click just hangs (or gets delayed) - subsequent clicks seem to only get actioned on every alternate click. Is this a bug? Am I doing something wrong? Any help at all would be appreciated. Here is the code simplified as much as possible.

NB if the call to notifyDatasetChanged is commented out then the onClick function is actioned every time as expected in 2.2

public class ButtonTest extends Activity implements OnClickListener {

private final static String TAG = "ButtonTestActivity";
private GridView layout;
private ImageAdapter imageAdapter;
private Button[] gameButtons ;

int count = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Context appContext = getApplicationContext();

    layout = new GridView(this);


    initGrid();

    /*button.setBackgroundColor(android.R.color.white);
    button.setTextColor(android.R.color.black);*/

    initAdapter(layout);


    setContentView(layout);
    count = 0;
}

private void initGrid() {
    Button button = new Button(layout.getContext());
    button.setText("press me 1");
    button.setId(3);
    button.setOnClickListener(this);

    Button button2 = new Button(layout.getContext());
    button2.setText("press me 2");
    button2.setId(4);
    button2.setOnClickListener(this);
    ((View) button).setVisibility(View.VISIBLE);
    gameButtons = new Button [2];
    gameButtons[0] = button;
    gameButtons[1] = button2;

}

private void initAdapter(GridView grid){
    imageAdapter = new ImageAdapter(this);
    grid.setAdapter(imageAdapter);
}

public void onClick(View v) {
    switch (v.getId()) {

    default: 
        Log.d(TAG,"view id = " + v.getId());
        count++;
        Log.d(TAG,"Count = " + count);
        Button temp = gameButtons[0];
        gameButtons[0] = gameButtons[1];
        gameButtons[1] = temp;
        Log.d(TAG,"buttons swapped");
        imageAdapter.notifyDataSetChanged();
    }
}

 public class ImageAdapter extends BaseAdapter {
        private Context mContext;


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

        public int getCount() {
            return gameButtons.length;
        }

        public Object getItem(int position) {
            return gameButtons[position];
        }

        public long getItemId(int position) {
            return gameButtons[position].getId();
        }

        // create a new ImageView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
            return gameButtons[position];
        }



    }

@Override
protected void onPause() {
    super.onPause();

}

@Override
protected void onResume() {
    super.onResume();
}

}