forked from camilor67/collect
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request getodk#480 from dcbriccetti/303-activities-checkab…
…le-lists getodk#303, Remove extra state and use Android APIs in activities with checkable lists getodk#441, Refactor preference classes
- Loading branch information
Showing
17 changed files
with
867 additions
and
902 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
collect_app/src/main/java/org/odk/collect/android/activities/AppListActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Copyright 2017 Nafundi | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.odk.collect.android.activities; | ||
|
||
import android.app.ListActivity; | ||
import android.widget.Button; | ||
import android.widget.ListView; | ||
|
||
import org.odk.collect.android.R; | ||
import org.odk.collect.android.application.Collect; | ||
import org.odk.collect.android.database.ActivityLogger; | ||
|
||
abstract class AppListActivity extends ListActivity { | ||
protected final ActivityLogger logger = Collect.getInstance().getActivityLogger(); | ||
|
||
protected boolean areCheckedItems() { | ||
return getCheckedCount() > 0; | ||
} | ||
|
||
/** Returns the IDs of the checked items, using the ListView of this activity. */ | ||
protected long[] getCheckedIds() { | ||
return getCheckedIds(getListView()); | ||
} | ||
|
||
/** Returns the IDs of the checked items, using the ListView provided */ | ||
protected long[] getCheckedIds(ListView lv) { | ||
// This method could be simplified by using getCheckedItemIds, if one ensured that | ||
// IDs were “stable” (see the getCheckedItemIds doc). | ||
int itemCount = lv.getCount(); | ||
int checkedItemCount = lv.getCheckedItemCount(); | ||
long[] checkedIds = new long[checkedItemCount]; | ||
int resultIndex = 0; | ||
for (int posIdx = 0; posIdx < itemCount; posIdx++) { | ||
if (lv.isItemChecked(posIdx)) { | ||
checkedIds [resultIndex] = lv.getItemIdAtPosition(posIdx); | ||
resultIndex++; | ||
} | ||
} | ||
return checkedIds; | ||
} | ||
|
||
/** Returns the IDs of the checked items, as an array of Long */ | ||
protected Long[] getCheckedIdObjects() { | ||
long[] checkedIds = getCheckedIds(); | ||
Long[] checkedIdObjects = new Long[checkedIds.length]; | ||
for (int i = 0; i < checkedIds.length; i++) { | ||
checkedIdObjects[i] = checkedIds[i]; | ||
} | ||
return checkedIdObjects; | ||
} | ||
|
||
protected int getCheckedCount() { | ||
return getListView().getCheckedItemCount(); | ||
} | ||
|
||
// toggles to all checked or all unchecked | ||
// returns: | ||
// true if result is all checked | ||
// false if result is all unchecked | ||
// | ||
// Toggle behavior is as follows: | ||
// if ANY items are unchecked, check them all | ||
// if ALL items are checked, uncheck them all | ||
public static boolean toggleChecked(ListView lv) { | ||
// shortcut null case | ||
if (lv == null) return false; | ||
|
||
boolean newCheckState = lv.getCount() > lv.getCheckedItemCount(); | ||
setAllToCheckedState(lv, newCheckState); | ||
return newCheckState; | ||
} | ||
|
||
public static void setAllToCheckedState(ListView lv, boolean check) { | ||
// no-op if ListView null | ||
if (lv == null) return; | ||
|
||
for (int x = 0; x < lv.getCount(); x++) { | ||
lv.setItemChecked(x, check); | ||
} | ||
} | ||
|
||
// Function to toggle button label | ||
public static void toggleButtonLabel(Button mToggleButton, ListView lv) { | ||
if (lv.getCheckedItemCount() != lv.getCount()) { | ||
mToggleButton.setText(R.string.select_all); | ||
} else { | ||
mToggleButton.setText(R.string.clear_all); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.