Download Android grid in reverse order

Whenever I change the orientation of the phone, I see that the order of the elements in the form of a grid is reversed, i.e. what was in the first position now ends, etc.

How can I get rid of this problem? Based on the clicked position, I invoke various actions. But since the order changes, incorrect actions are triggered when the screen orientation changes.

I added android:configChanges="orientation|keyboardHidden" to the 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(); } } }); 
+4
source share
1 answer

That v = convertView looks suspicious. I think you need to update the view with the required image and text for this position. (Otherwise, you simply reuse the cached view, which may well be served in the wrong order when restoring the grid.)

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

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

+7
source

All Articles