1

I have an application with a Listview that I insert a new row by a buttom, but when I rotate the phone, all inside the ListView disappear, and I have tried to retain the data and recreate the ListView, but whitout success... The array userArray with the correct data, but when I call the userAdapter.notifyDataSetChanged(); the ListView is not filled with the data...

public class Page1 extends Fragment
{
    ListView userList;
        private ItemsListAdapter userAdapter=null;
        ArrayList<User> userArray = new ArrayList<User>();


        @Override
        public void onSaveInstanceState(Bundle outState) {
            Log.d(TAG, "onSaveInstanceState");
            super.onSaveInstanceState(outState);
            outState.putParcelableArrayList("key_userArray", userArray);
            outState.putParcelable("key_ListView", userList.onSaveInstanceState());

        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            if (savedInstanceState != null) {
                // Restore last state.
                userArray = savedInstanceState.getParcelableArrayList("key_userArray");  
            }
        }    

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            rootView = inflater.inflate(R.layout.page1, container, false);
            Log.d(TAG, "onCreateView");  

            appContext = getActivity().getBaseContext();
            appContextDialog = getActivity();
            GappContext = appContext.getApplicationContext();
            dBRicette = new ItemsSave();
            thisFrag = this;
            userList = null;
            userListIng = null;         
            //userAdapter = null;         
            userAdapterIng = null;
            myPref.loadPreference(appContext,MainActivity.DATABASE_INGR, MainActivity.ACCOUNT_PREFS_NAME);  

            init(rootView, savedInstanceState); 

            String myTag = getTag();        
            ((MainActivity)getActivity()).setTabFragPage1(myTag);
            int val = InitListaIngredienti();
            if(val == 0){
            }else
                Toast.makeText(getActivity().getBaseContext(), getResources().getString(R.string.msg_problem_db), Toast.LENGTH_LONG).show();

            return rootView;
        }

       void init(View rootView, Bundle savedInstanceState){
            if(userAdapter == null ){
                if(savedInstanceState == null || !savedInstanceState.containsKey("key_userArray")) {
                    userArray = new ArrayList<User>();
                    //userAdapter = new UserCustomAdapter(getActivity().getBaseContext(), R.layout.row, userArray);
                    userAdapter = new ItemsListAdapter(getActivity().getBaseContext(), userArray);
                    userList = (ListView) rootView.findViewById(R.id.listView);
                        userList.setItemsCanFocus(false);
                        userList.setClickable(true);
                        userList.setLongClickable(true);


                        userList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

                            public boolean onItemLongClick(AdapterView<?> arg0, View v, int position, long id) {
                                // TODO Auto-generated method stub
                                RelativeLayout listItem = (RelativeLayout) v;
                                TextView clickedItemView = (TextView) listItem.findViewById(R.id.tv_prodotto);
                                String clickedItemString = clickedItemView.getText().toString();
                                Log.v(TAG,"DisplayListCustom LongClick detected " + clickedItemString + ", position " + Integer.toString(position));
                                if (currentActionMode != null) { 
                                    return false; 
                                }

                                currentListItemIndex = position;//(User)v.getTag();
                                currentActionMode = getActivity().startActionMode(modeCallBack);
                                v.setSelected(true);
                                return true;
                            }
                        }); 

                        //userList.setOnItemSelectedListener(this);
                        userAdapter.notifyDataSetChanged();
                        userList.setAdapter(userAdapter);  
                }else {
                    //recupero i valori
                    mListInstanceState = savedInstanceState.getParcelable("key_ListView");
                    userArray = savedInstanceState.getParcelableArrayList("key_userArray");
                    userAdapter = new ItemsListAdapter(getActivity().getBaseContext(), userArray);
                    userList = (ListView) rootView.findViewById(R.id.listView);
                    userList.onRestoreInstanceState(mListInstanceState);
                    userList.setAdapter(userAdapter);
//                  int size = userArray.size();
//                  for(int i = 0; i < size; i++){
//                      userAdapter.UpdateIng(userArray.get(i).qta, userArray.get(i).name, TotVar);
//                  }
                    userAdapter.notifyDataSetChanged();
                }
            }else        
                userAdapter.notifyDataSetChanged();   


class ItemsListAdapter extends BaseAdapter {

     private LayoutInflater vi;
     private ArrayList<User> data = new ArrayList<User>();
     Context context;
     User tmp = new User();
     NumberFormat numberFormat0  = new DecimalFormat("#0.0");

     public ItemsListAdapter(Context context, /*int layoutResourceId,*/ ArrayList<User> data) {
        super();
        vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.data = data;
        this.context = context;
     }

     /** Add white line */
     public void addItem(User item) {
        data.add(item);
         notifyDataSetChanged();
     }

     public ArrayList<User> getData() {
        return data;
    }

    public void setData(ArrayList<User> data) {
        this.data = data;
        notifyDataSetChanged();
    }

     @Override
     public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

         if (convertView == null) {

            holder = new ViewHolder();
            convertView = vi.inflate(R.layout.row3, parent, false);//null);

            holder.grid = (GridLayout) convertView.findViewById(R.id.GridLayout);
            holder.row = (TextView) convertView.findViewById(R.id.tv_row);
            holder.name = (TextView) convertView.findViewById(R.id.tv_prodotto);
            holder.quantity = (TextView) convertView.findViewById(R.id.tv_qta);
            holder.textPercT = (TextView) convertView.findViewById(R.id.tv_perc_T);
            holder.textPercZ = (TextView) convertView.findViewById(R.id.tv_perc_Z);
            holder.textPercG = (TextView) convertView.findViewById(R.id.tv_perc_G);
            holder.textUm = (TextView) convertView.findViewById(R.id.tv_grm);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
Andrea Bandiera
  • 109
  • 1
  • 3
  • 14

2 Answers2

0

This is happening because onCreat gets called on orientation changes there is a simply way to stop calling onCreate.

Just add configChanges attribute to your AndroidManifest.xml, like that:

For more Information Refer to the following posts

Activity restart on rotation Android

How do I disable orientation change on Android?

http://developer.android.com/guide/topics/manifest/activity-element.html#config

Community
  • 1
  • 1
Umer Kiani
  • 3,783
  • 5
  • 36
  • 63
0

it is too much stuff for onCreateView. This method should return asap with the inflated view. When you rotate the screen, you should simply recreate the adapater with the dataset you retrieved in the bundle, recreate the adapter and call again setAdapter on the ListView's instance. Activity uses the pair onSavedInstanceState/onRestoreInstanceState. As you noticed Fragment has not onRestoreInstancedState. You should use onActivityCreated on its behalf

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • yes, there is too much stuff inside, but how can divide that? there are better way? – Andrea Bandiera Jul 27 '14 at 18:03
  • No, you probably can't. But you can use the other callback in order to lightweight onCreateView. Ideally, `onCreateView` should consist only of the return statement. You can use onViewCreated for the other purpose – Blackbelt Jul 27 '14 at 18:05
  • the problem , I think, is the refresh of ListView, because the array and the adapter have the correct data inside it, but don't show. The method **getView** inside the **ItemsListAdapter** isn't called. I have edit my question with the code of **ItemsListAdapter**. – Andrea Bandiera Jul 28 '14 at 19:32
  • I am not sure what is going on.. I am sorry – Blackbelt Jul 28 '14 at 19:37
  • I have debug the program and the data are retrieve correctly from the Bundle, but inside the method getView() inside the ItemsListAdapter isn't called, it's only called before the rotation, when all work fine. – Andrea Bandiera Jul 28 '14 at 19:51