Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android grid view loads in reverse order

Whenever I change the orientation of the phone, I see the order of items in grid view reverses i.e. what was at the first position now comes at the end and so on.

How can I get rid of this issue? Based on the position clicked, I am invoking different activities. But because the order changes, wrong activities are being called when the screen orientation is changed.

I have added android:configChanges="orientation|keyboardHidden" in my manifest file.

Any help is appreciated.

Adapter Class:

class ImageAdapter extends BaseAdapter {

    Context mContext;

    private String[] mHome_icon_text = { "A", "B",
            "C", "D", "E","F" };

    private Integer[] mHome_icon_image = { R.drawable.icon,
            R.drawable.icon, R.drawable.icon, R.drawable.icon,
            R.drawable.icon, R.drawable.icon, };

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

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mHome_icon_text.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View v;
        if (convertView == null) {
            LayoutInflater li = getLayoutInflater();
            v = li.inflate(R.layout.home_grid_view_item, null);
            TextView tv = (TextView) v.findViewById(R.id.home_icon_text);
            tv.setText(mHome_icon_text[position]);
            ImageView iv = (ImageView) v.findViewById(R.id.home_icon_image);

            iv.setImageResource(mHome_icon_image[position]);

        } else {
            v = convertView;
        }
        return v;
    }
}

In the main class:

grid_main.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v,
            int position, long id) {
        Intent i;
        switch (position) {
        case 0:
            // start A
            i = new Intent(Home.this,
                    com.da.A.class);
            startActivity(i);
            break;

        case 3: //Cactivity
            i = new Intent(Home.this,
                    com.da.C.class);
            startActivity(i);
            break;
        default:
            Toast
                    .makeText(Home.this, "" + position,
                            Toast.LENGTH_SHORT).show();
        }

    }
});
like image 885
Sapan Avatar asked Jun 08 '11 11:06

Sapan


1 Answers

That v = convertView looks suspicious. I think you need to update the view with the required image and text for that position. (Otherwise you're simply re-using a cached view which may well be fed to you in the wrong order when the grid is rebuilt.)

1) As a test, try taking the if (convertView == null) { branch even if it is not null and see if that fixes it. (This will unnecessarily inflate new views when scrolling or rotating, so don't leave it like this.)

2) If that does fix it, you can correctly recycle a previously inflated convertView by finding your R.id.home_icon_text etc within it, and updating it for the current position in the convertView != null branch.

like image 172
Martin Stone Avatar answered Oct 06 '22 10:10

Martin Stone