在设备轮换时更新列表视图

时间:2017-12-07 10:57:08

标签: android listview search android-edittext rotation

当我第二次按下搜索按钮时,列表视图不会更新。 当我将手机从水平位置旋转到垂直位置时,应用程序会保存状态,但是当我将应用程序从垂直位置旋转到水平位置时,它不会。 你能帮助我吗?

MainActivity:

            public class MainActivity extends AppCompatActivity implements LoaderCallbacks<List<Book>> {
            private static final String LOG_TAG = MainActivity.class.getName();

        private static final String GOOGLEBOOK_REQUEST_URL =
                "https://www.googleapis.com/books/v1/volumes?q=";
        private static final String URL_FIX = "&maxResults=10";


        private static final int BOOK_LOADER_ID = 1;

        private String googleBookRequest;
        private BookAdapter mAdapter;
        private ListView listView;
        private EditText searchBar;

        /** TextView that is displayed when the list is empty */
        private TextView mEmptyStateTextView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

             listView = (ListView) findViewById(R.id.list);

            mEmptyStateTextView = (TextView) findViewById(R.id.empty_view);
            listView.setEmptyView(mEmptyStateTextView);

            mAdapter = new BookAdapter(this, new ArrayList<Book>());
            final LoaderManager loaderManager = getLoaderManager();
            listView.setAdapter(mAdapter);
            searchBar = (EditText) findViewById(R.id.search_view);
            ImageButton button = (ImageButton) findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // Get a reference to the ConnectivityManager to check state of network connectivity
                    ConnectivityManager connMgr = (ConnectivityManager)
                            getSystemService(Context.CONNECTIVITY_SERVICE);
    googleBookRequest="";
                    // Get details on the currently active default data network
                    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
                    // If there is a network connection, fetch data
                    if (networkInfo != null && networkInfo.isConnected()) {
                        // Get a reference to the LoaderManager, in order to interact with loaders.
                        LoaderManager loaderManager = getLoaderManager();
                        googleBookRequest = createSearchUrl(searchBar);

                        // Initialize the loader. Pass in the int ID constant defined above and pass in null for
                        // the bundle. Pass in this activity for the LoaderCallbacks parameter (which is valid
                        // because this activity implements the LoaderCallbacks interface).
                        loaderManager.initLoader(BOOK_LOADER_ID, null, MainActivity.this);
                    } else {
                        // Otherwise, display error
                        // First, hide loading indicator so error message will be visible
                        View loadingIndicator = findViewById(R.id.loading_indicator);
                        loadingIndicator.setVisibility(View.GONE);

                        // Update empty state with no connection error message
                        mEmptyStateTextView.setText(R.string.no_internet_connection);
                    }

                }
            });



        }
        @Override
        public Loader<List<Book>> onCreateLoader(int i, Bundle bundle) {
            // Create a new loader for the given URL
            return new BookLoader(this, googleBookRequest);
        }

        @Override
        public void onLoadFinished(Loader<List<Book>> loader, List<Book> books) {
            // Hide loading indicator because the data has been loaded
            View loadingIndicator = findViewById(R.id.loading_indicator);
            loadingIndicator.setVisibility(View.GONE);
            mAdapter.clear();
            if (books != null && !books.isEmpty()) {
                mAdapter.addAll(books);
            } else{
                // Set empty state text to display "No earthquakes found."
                mEmptyStateTextView.setText(R.string.no_books);}



        }

        @Override
        public void onLoaderReset(Loader<List<Book>> loader) {
            // Loader reset, so we can clear out our existing data.
            mAdapter.clear();
        }



        private String createSearchUrl(EditText editText) {
            String search = editText.getText().toString().toLowerCase().trim();;
            StringBuilder url = new StringBuilder(GOOGLEBOOK_REQUEST_URL);
            return url.append(search.replace(" ", "+")).append(URL_FIX).toString();
        }
    }

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.booklistingapp">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"
            android:configChanges="orientation">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

0 个答案:

没有答案