+ // We need to use the `SupportActionBar` from `android.support.v7.app.ActionBar` until the minimum API is >= 21.
+ final Toolbar bookmarksAppBar = (Toolbar) findViewById(R.id.domains_toolbar);
+ setSupportActionBar(bookmarksAppBar);
+
+ // Display the home arrow on `SupportActionBar`.
+ ActionBar appBar = getSupportActionBar();
+ assert appBar != null;// This assert removes the incorrect warning in Android Studio on the following line that `appBar` might be null.
+ appBar.setDisplayHomeAsUpEnabled(true);
+
+ // Initialize the database handler. `this` specifies the context. The two `nulls` do not specify the database name or a `CursorFactory`.
+ // The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
+ domainsDatabaseHelper = new DomainsDatabaseHelper(this, null, null, 0);
+
+ // Determine if we are in two pane mode. `domains_list_framelayout` is only populated if two panes are present.
+ twoPaneMode = ((findViewById(R.id.domains_list_framelayout)) != null);
+
+ // Initialize `domainsListView`.
+ domainsListView = (ListView) findViewById(R.id.domains_listview);
+
+ domainsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
+ @Override
+ public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
+ // Convert the id from long to int to match the format of the domains database.
+ int databaseId = (int) id;
+
+ // Get the database `Cursor` for this ID and move it to the first row.
+ Cursor domainCursor = domainsDatabaseHelper.getCursorForId(databaseId);
+ domainCursor.moveToFirst();
+
+ // If the
+ }
+ });
+
+ FloatingActionButton addDomainFAB = (FloatingActionButton) findViewById(R.id.add_domain_fab);
+ addDomainFAB.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View view) {
+ // Show the `AddDomain` `AlertDialog` and name the instance `@string/add_domain`.
+ AppCompatDialogFragment addDomainDialog = new AddDomain();
+ addDomainDialog.show(getSupportFragmentManager(), getResources().getString(R.string.add_domain));
+ }
+ });
+
+ // Load the `ListView`.
+ updateDomainsListView();
+ }
+
+ @Override
+ public void onAddDomain(AppCompatDialogFragment dialogFragment) {
+ // Get the `domainNameEditText` from `dialogFragment` and extract the string.
+ EditText domainNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.domain_name_edittext);
+ String domainNameString = domainNameEditText.getText().toString();
+
+ // Create the domain.
+ domainsDatabaseHelper.addDomain(domainNameString);
+
+ // Refresh the `ListView`.
+ updateDomainsListView();
+ }
+
+ private void updateDomainsListView() {
+ // Get a `Cursor` with the current contents of the domains database.
+ Cursor domainsCursor = domainsDatabaseHelper.getCursorOrderedByDomain();
+
+ // Setup `domainsCursorAdapter` with `this` context. `false` disables `autoRequery`.
+ CursorAdapter domainsCursorAdapter = new CursorAdapter(this, domainsCursor, false) {
+ @Override
+ public View newView(Context context, Cursor cursor, ViewGroup parent) {
+ // Inflate the individual item layout. `false` does not attach it to the root.
+ return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
+ }
+
+ @Override
+ public void bindView(View view, Context context, Cursor cursor) {
+ // Set the domain name.
+ String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN));
+ TextView domainNameTextView = (TextView) view.findViewById(R.id.domain_name_textview);
+ domainNameTextView.setText(domainNameString);
+ }
+ };
+
+ // Update the `RecyclerView`.
+ domainsListView.setAdapter(domainsCursorAdapter);