Example usage for android.widget ListView onRestoreInstanceState

List of usage examples for android.widget ListView onRestoreInstanceState

Introduction

In this page you can find the example usage for android.widget ListView onRestoreInstanceState.

Prototype

@Override
    public void onRestoreInstanceState(Parcelable state) 

Source Link

Usage

From source file:de.measite.contactmerger.MergeActivity.java

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    if (savedInstanceState.containsKey("MERGELIST")) {
        ListView list = (ListView) findViewById(R.id.contact_merge_list);
        list.onRestoreInstanceState(savedInstanceState.getParcelable("MERGELIST"));
    }//ww w  .java 2s . com
    updateList();
}

From source file:org.solovyev.android.messenger.BaseListFragment.java

protected void restoreListViewState() {
    final ListView listView = getListViewById();
    final Parcelable listViewState = restoredListViewState;
    if (listView != null) {
        Log.d(tag, "Restoring list view(size=" + listView.getCount() + "): " + listViewState);
        if (listViewState != null) {
            listView.onRestoreInstanceState(listViewState);
        } else {/* w ww  . ja va 2 s  . c o  m*/
            if (isScrollToEndByDefault()) {
                final int count = getAdapter().getCount();
                if (count > 0) {
                    listView.setSelection(count - 1);
                }
            }
        }
    }
}

From source file:com.murati.oszk.audiobook.ui.MediaBrowserFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    LogHelper.d(TAG, "fragment.onCreateView");

    //TODO: rethink mMediaId in instance context
    final String mediaId = getMediaId();

    // Update Favoritebutton on backstack navigate
    try {//from  w  w w .java 2 s  .c o  m
        ((MusicPlayerActivity) this.getActivity()).updateBookButtons(mediaId);
    } catch (Exception ex) {
        Log.e(TAG, ex.getMessage());
    }

    View rootView = inflater.inflate(R.layout.fragment_list, container, false);

    mErrorView = rootView.findViewById(R.id.playback_error);
    mErrorMessage = (TextView) mErrorView.findViewById(R.id.error_message);

    mBrowserAdapter = new BrowseAdapter(getActivity());

    final ListView listView = (ListView) rootView.findViewById(R.id.list_view);
    listView.setAdapter(mBrowserAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            checkForUserVisibleErrors(false);
            if (mediaId != null) {
                //TODO: listview restore position
                Parcelable state = listView.onSaveInstanceState();
                if (listState.containsKey(mediaId)) {
                    ((ConcurrentHashMap) listState).remove(mediaId);
                }
                ((ConcurrentHashMap) listState).put(mediaId, state);
            }
            MediaBrowserCompat.MediaItem item = mBrowserAdapter.getItem(position);
            mMediaFragmentListener.onMediaItemSelected(item);
        }
    });

    // Restore listViewState if we have previous
    if (mediaId != null && listState.containsKey(mediaId)) {
        listView.onRestoreInstanceState(listState.get(mediaId));
    }
    return rootView;
}

From source file:com.nachtimwald.android.serviceexplorer.ServiceListFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_servicelist, container, false);
    ListView lv = (ListView) view.findViewById(R.id.serviceList);

    List<Map<String, String>> serviceData = new ArrayList<>();

    if (getArguments().getBoolean("allServices")) {
        for (PackageInfo packageInfo : getActivity().getPackageManager()
                .getInstalledPackages(PackageManager.GET_SERVICES)) {
            if (packageInfo.services == null) {
                continue;
            }/*  w  ww.  jav a 2s .c o m*/
            for (ServiceInfo serviceInfo : packageInfo.services) {
                Map<String, String> datum = new HashMap<>();
                datum.put("name", serviceInfo.name);
                datum.put("package", serviceInfo.packageName);
                datum.put("search", serviceInfo.name.replace(".", " ").replace("$", " "));
                serviceData.add(datum);
            }
        }
    } else {
        ActivityManager activityManager = (ActivityManager) getActivity()
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> runningServiceInfos = activityManager
                .getRunningServices(Integer.MAX_VALUE);
        for (ActivityManager.RunningServiceInfo info : runningServiceInfos) {
            Map<String, String> datum = new HashMap<>();
            datum.put("name", info.service.getClassName());
            datum.put("package", info.service.getPackageName());
            datum.put("search", info.service.getClassName().replace(".", " ").replace("$", " "));
            serviceData.add(datum);
        }
    }

    mServicesAdapter = new SimpleAdapter(getActivity(), serviceData, android.R.layout.simple_list_item_2,
            new String[] { "name", "package", "search" },
            new int[] { android.R.id.text1, android.R.id.text2, 0 });
    lv.setAdapter(mServicesAdapter);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent i = new Intent(getActivity(), DetailActivity.class);
            i.putExtra("name", ((HashMap<String, String>) parent.getItemAtPosition(position)).get("name"));
            i.putExtra("package",
                    ((HashMap<String, String>) parent.getItemAtPosition(position)).get("package"));
            startActivity(i);
        }
    });

    if (savedInstanceState != null) {
        lv.onRestoreInstanceState(savedInstanceState.getParcelable(STATE_LISTPOS));
    }

    return view;
}

From source file:com.juick.android.MessagesFragment.java

private void init(final boolean moveToTop) {
    if (implicitlyCreated)
        return;//from w ww .  java2  s .  c o m

    parent.imagePreviewHelper = listAdapter.imagePreviewHelper = new ImagePreviewHelper(
            (ViewGroup) getView().findViewById(R.id.imagepreview_container), parent);

    final MessageListBackingData savedMainList = JuickAdvancedApplication.instance.getSavedList(getActivity());
    final ListView lv = getListView();
    boolean canUseMainList = getActivity() instanceof MainActivity; //
    if (savedMainList != null && canUseMainList) {
        messagesSource = savedMainList.messagesSource;
        initListWithMessages(savedMainList.messages);
        int selectItem = 0;
        ListAdapter wrappedAdapter = lv.getAdapter();
        for (int i = 0; i < wrappedAdapter.getCount(); i++) {
            Object ai = wrappedAdapter.getItem(i);
            if (ai != null && ai instanceof JuickMessage) {
                if (((JuickMessage) ai).getMID().equals(savedMainList.topMessageId)) {
                    selectItem = i;
                }
            }
        }
        lv.setSelectionFromTop(selectItem, savedMainList.topMessageScrollPos);
        JuickAdvancedApplication.instance.setSavedList(null, false);
    } else {
        final MessagesLoadNotification messagesLoadNotification = new MessagesLoadNotification(getActivity(),
                handler);
        Thread thr = new Thread("Download messages (init)") {

            public void run() {
                final MessagesLoadNotification notification = messagesLoadNotification;
                final Utils.Function<Void, RetainedData> then = new Utils.Function<Void, RetainedData>() {
                    @Override
                    public Void apply(final RetainedData mespos) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                notification.statusText.setText("Filter and format..");
                            }
                        });
                        Log.w("com.juick.advanced", "getFirst: before filter");
                        final ArrayList<JuickMessage> messages = filterMessages(mespos.messages);
                        Log.w("com.juick.advanced", "getFirst: after filter");
                        Boolean ownView = null;
                        if (!JuickMessagesAdapter.dontKeepParsed(parent)) {
                            for (JuickMessage juickMessage : messages) {
                                if (ownView == null) {
                                    MicroBlog blog = MainActivity.microBlogs
                                            .get(juickMessage.getMID().getMicroBlogCode());
                                    ownView = blog instanceof OwnRenderItems;
                                }
                                if (!ownView) {
                                    juickMessage.parsedText = JuickMessagesAdapter.formatMessageText(parent,
                                            juickMessage, false);
                                }
                            }
                        }
                        final Parcelable listPosition = mespos.viewState;
                        if (isAdded()) {
                            if (messages.size() == 0) {
                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (notification.lastError == null) {
                                            notification.statusText
                                                    .setText(parent.getString(R.string.EmptyList));
                                        } else {
                                            notification.statusText.setText(
                                                    "Error obtaining messages: " + notification.lastError);

                                        }
                                        notification.progressBar.setVisibility(View.GONE);
                                    }
                                });
                            }
                            final Activity activity = getActivity();
                            if (activity != null) {
                                final Parcelable finalListPosition = listPosition;
                                activity.runOnUiThread(new Runnable() {

                                    public void run() {
                                        try {
                                            if (isAdded()) {
                                                lastPrepareMessages(messages, new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        if (!hasListView()) {
                                                            handler.postDelayed(this, 300);
                                                            return;
                                                        }
                                                        initListWithMessages(messages);
                                                        if (moveToTop) {
                                                            lv.setSelection(0);
                                                        } else {
                                                            if (finalListPosition != null) {
                                                                lv.onRestoreInstanceState(finalListPosition);
                                                            } else {
                                                                //setSelection(messagesSource.supportsBackwardRefresh() ? 1 : 0);
                                                                setSelection(0);
                                                            }
                                                        }
                                                        Log.w("com.juick.advanced", "getFirst: end.");
                                                        handler.postDelayed(new Runnable() {
                                                            @Override
                                                            public void run() {
                                                                onListLoaded();
                                                            }
                                                        }, 10);
                                                    }
                                                });
                                            }
                                        } catch (IllegalStateException e) {
                                            Toast.makeText(activity, e.toString(), Toast.LENGTH_LONG).show();
                                        }
                                    }
                                });
                            }
                        } else {
                            Log.w("com.juick.advanced", "getFirst: not added!");
                        }
                        return null;
                    }
                };
                if (getActivity() != null)
                    messagesSource.setContext(getActivity());
                if (restoreData == null) {
                    messagesSource.getFirst(notification, new Utils.Function<Void, ArrayList<JuickMessage>>() {
                        @Override
                        public Void apply(ArrayList<JuickMessage> juickMessages) {
                            return then.apply(new RetainedData(juickMessages, null));
                        }
                    });
                } else {
                    then.apply((RetainedData) restoreData);
                    restoreData = null;
                }
            }
        };
        thr.setPriority(Thread.MIN_PRIORITY);
        thr.start();
    }
}