在onresume之后丢失了setOnItemLongClickListener

时间:2014-06-14 11:22:32

标签: android listview onresume

我创建了一个ListView并设置了例程setOnItemLongClickListener,当我更改Fragment时,我创建了一个例程onStop(),用于在onResume()之后保存数据以进行恢复。 但是我已经尝试恢复ListView,但是ListView中的数据(userArray中的数据)是正确的,但没有出现,代码错误在哪里?

void init(View rootView){

            if(userAdapter == null){
                //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
}

public void onResume() {
                super.onResume();
                Log.i(TAG, "On Resume .....");
                //chiamata al ritorno alla Pagina2
                SaveDataPag2 tData = ((MainActivity)getActivity()).getSaveToDataPage2();

                if(tData != null){              
                    userArray = tData.userArray;
                    //setMessage(tData);
                    //userAdapter.notifyDataSetChanged();
                    userAdapter = new ItemsListAdapter(getActivity().getBaseContext(), userArray);
                    //userAdapter =tData.userAdapter;
                    userList = (ListView) rootView.findViewById(R.id.listView);
                    //userList = tData.userList;                    
                    userList.setAdapter(userAdapter);                   
                    modeCallBack = tData.ModeCallBack;
                    userAdapter.notifyDataSetChanged();
                }
            }


public void onStop() {
                super.onStop();
                Log.i(TAG, "On Stop .....");

               SaveDataPag2 SPag2 = new SaveDataPag2();
               SPag2.userList = userList;
               SPag2.userArray = userArray;
               SPag2.ModeCallBack = modeCallBack;
               SPag2.userAdapter = userAdapter;
              ((MainActivity)getActivity()).putSaveToDataPage2(SPag2);
            }

0 个答案:

没有答案