下面列出了android.widget.ListAdapter#getItem ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/** Taps the specified preference displayed by the provided Activity. */
@FixWhenMinSdkVersion(11)
@SuppressWarnings("deprecation")
public static void tapPreference(PreferenceActivity activity, Preference preference) {
// IMPLEMENTATION NOTE: There's no obvious way to find out which View corresponds to the
// preference because the Preference list in the adapter is flattened, whereas the View
// hierarchy in the ListView is not.
// Thus, we go for the Reflection-based invocation of Preference.performClick() which is as
// close to the invocation stack of a normal tap as it gets.
// Only perform the click if the preference is in the adapter to catch cases where the
// preference is not part of the PreferenceActivity for some reason.
ListView listView = activity.getListView();
ListAdapter listAdapter = listView.getAdapter();
for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
if (listAdapter.getItem(i) == preference) {
invokePreferencePerformClickOnMainThread(preference, activity.getPreferenceScreen());
return;
}
}
throw new IllegalArgumentException("Preference " + preference + " not in list");
}
@Override
public void onPause() {
super.onPause();
SharedPreferences.Editor editor = getSharedPreferences(PREFSFILE, 0).edit();
editor.putBoolean(ALWAYS_GOOGLE_PLAY,
((CheckBox) findViewById(R.id.always_gplay)).isChecked());
if (template != null) {
editor.putLong(TEMPLATEID, template.id);
}
ListAdapter adapter = getListAdapter();
int count = adapter.getCount();
for (int i = 0; i < count; i++) {
SortablePackageInfo spi = (SortablePackageInfo) adapter.getItem(i);
editor.putBoolean(SELECTED + "." + spi.packageName, spi.selected);
}
editor.commit();
}
/**
* Share with the world.
*/
private void doStumble() {
ListAdapter adapter = getListAdapter();
int count = adapter.getCount();
ArrayList<String> collect = new ArrayList<String>();
for (int i = 0; i < count; i++) {
SortablePackageInfo spi = (SortablePackageInfo) adapter.getItem(i);
if (spi.selected) {
collect.add(spi.packageName);
}
}
Collections.shuffle(collect);
StringBuilder sb = new StringBuilder();
for (int i=0;i<collect.size();i++) {
if (sb.length()>0) {
sb.append(",");
}
sb.append(collect.get(i));
if (sb.length()>200) {
break; // prevent the url from growing overly large.
}
}
openUri(this,Uri.parse(getString(R.string.url_browse,sb.toString())));
}
/**
* Check if at least one app is selected. Pop up a toast if none is selected.
*
* @return true if no app is selected.
*/
public boolean isNothingSelected() {
ListAdapter adapter = getListAdapter();
if (adapter != null) {
int count = adapter.getCount();
for (int i = 0; i < count; i++) {
SortablePackageInfo spi = (SortablePackageInfo) adapter.getItem(i);
if (spi.selected) {
return false;
}
}
}
Toast.makeText(this, R.string.msg_warn_nothing_selected, Toast.LENGTH_LONG)
.show();
return true;
}
/**
*
* @param adapter
* The listadapter (containing the list of apps) to control.
*/
public TagSelectionListener(ListAdapter adapter) {
this.adapter = adapter;
int count = adapter.getCount();
Vector<String> collect = new Vector<String>();
for (int i = 0; i < count; i++) {
SortablePackageInfo spi = (SortablePackageInfo) adapter.getItem(i);
String[] tags = MainActivity.noNull(spi.tags).split(",");
for (String tag : tags) {
String tmp = tag.trim();
if (tmp.length() > 0 && !collect.contains(tmp)) {
collect.add(tmp);
}
}
}
items = collect.toArray(new String[0]);
states = new boolean[items.length];
Arrays.sort(items);
}
/**
* Get the data item associated with the specified position in the data set.
*
* @param position Position of the item whose data we want
*/
@Override
public Object getItem(int position) {
for (ListAdapter piece : pieces) {
int size = piece.getCount();
if (position < size) {
return (piece.getItem(position));
}
position -= size;
}
return (null);
}
public Object getItem(int position) {
int size;
for (Iterator i$ = this.getPieces().iterator(); i$.hasNext(); position -= size) {
ListAdapter piece = (ListAdapter) i$.next();
size = piece.getCount();
if (position < size) {
return piece.getItem(position);
}
}
return null;
}
/**
* Get the data item associated with the specified
* position in the data set.
*
* @param position
* Position of the item whose data we want
*/
@Override
public Object getItem(int position) {
for (ListAdapter piece : getPieces()) {
int size=piece.getCount();
if (position < size) {
return(piece.getItem(position));
}
position-=size;
}
return(null);
}
/**
* Get the data item associated with the specified position in the data set.
* @param position Position of the item whose data we want
*/
public Object getItem(int position) {
for (ListAdapter piece : pieces) {
int size = piece.getCount();
if (position < size) {
return (piece.getItem(position));
}
position -= size;
}
return (null);
}
private Preference getPreferenceFromMenuInfo(ContextMenuInfo menuInfo) {
if (menuInfo != null) {
if (menuInfo instanceof AdapterContextMenuInfo) {
AdapterContextMenuInfo adapterMenuInfo = (AdapterContextMenuInfo) menuInfo;
PreferenceScreen screen = getPreferenceScreen();
ListAdapter root = screen.getRootAdapter();
Object item = root.getItem(adapterMenuInfo.position);
return (Preference) item;
}
}
return null;
}
/**
* Get the data item associated with the specified position in the data set.
*
* @param position Position of the item whose data we want
*/
public Object getItem(int position) {
for (ListAdapter piece : pieces) {
int size = piece.getCount();
if (position < size) {
return piece.getItem(position);
}
position -= size;
}
return null;
}
@Override
public void onItemClick(ListAdapter parent, View view, int position, long id) {
// Utils.startHeroDetailActivity(this.getActivity(),
// (HeroDetailItem) parent.getItemAtPosition(position));
final Object cItem = parent.getItem(position);
if (cItem instanceof ItemsItem) {
Utils.startItemsDetailActivity(this.getActivity(),
(ItemsItem) cItem);
}
}
@Test
public void testReorderAccounts() {
accountDb.add(
"first", "7777777777777777", OtpType.HOTP, null, null, AccountDb.GOOGLE_ISSUER_NAME);
accountDb.add("second", "6666666666666666", OtpType.TOTP, null, null, null);
accountDb.add("third", "5555555555555555", OtpType.TOTP, null, null, "Yahoo");
activityTestRule.launchActivity(null);
EmptySpaceClickableDragSortListView userList =
activityTestRule.getActivity().findViewById(R.id.user_list);
View firstView = userList.getChildAt(0);
View thirdView = userList.getChildAt(2);
View firstDragHandle = firstView.findViewById(R.id.user_row_drag_handle);
View thirdDragHandle = thirdView.findViewById(R.id.user_row_drag_handle);
int dragHandleWidth = firstDragHandle.getWidth();
int dragHandleHeight = firstDragHandle.getHeight();
int[] firstDragHandleLocation = new int[2];
int[] thirdDragHandleLocation = new int[2];
firstDragHandle.getLocationOnScreen(firstDragHandleLocation);
thirdDragHandle.getLocationOnScreen(thirdDragHandleLocation);
float fromX = firstDragHandleLocation[0] + (dragHandleWidth / 2.0f);
float fromY = firstDragHandleLocation[1] + (dragHandleHeight / 2.0f);
float toX = thirdDragHandleLocation[0] + (dragHandleWidth / 2.0f);
float toY = thirdDragHandleLocation[1] + (dragHandleHeight / 2.0f);
onView(equalTo(firstView))
.perform(longClick())
.perform(
new GeneralSwipeAction(
Swipe.SLOW,
(view) -> {
return new float[] {fromX, fromY};
},
(view) -> {
return new float[] {toX, toY};
},
Press.FINGER));
ListAdapter listAdapter = userList.getAdapter();
PinInfo firstPinInfo = (PinInfo) listAdapter.getItem(0);
PinInfo secondPinInfo = (PinInfo) listAdapter.getItem(1);
PinInfo thirdPinInfo = (PinInfo) listAdapter.getItem(2);
assertThat(firstPinInfo.getIndex().getName()).isEqualTo("second");
assertThat(secondPinInfo.getIndex().getName()).isEqualTo("third");
assertThat(thirdPinInfo.getIndex().getName()).isEqualTo("first");
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
ListAdapter adapter = appListFragment.getListAdapter();
AppEntry appEntry = (AppEntry) adapter.getItem(selectedItem);
if (appEntry == null) {
Toast.makeText(this, R.string.error_no_app_entry, Toast.LENGTH_SHORT).show();
return true;
}
Intent intent = new Intent(this, WebViewActivity.class);
intent.putExtra(Intent.EXTRA_TEXT, appEntry.getLabel());
switch (item.getItemId()) {
case android.R.id.home:
setResult(RESULT_CANCELED);
finish();
return true;
case R.id.details:
showDetailView(appEntry, intent);
return true;
case R.id.generate_pin:
generatePin(appEntry, intent);
return true;
case R.id.save:
saveCertificate(appEntry, intent);
return true;
case R.id.virustotal:
virustotal(appEntry, intent);
return true;
case R.id.by_apk_hash:
byApkHash(appEntry, intent);
return true;
case R.id.by_package_name:
byPackageName(appEntry, intent);
return true;
case R.id.by_signing_certificate:
bySigningCertificate(appEntry, intent);
return true;
case R.id.action_settings:
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* Construct what is to be shared/copied to the clipboard
*
* @return the output for sharing.
*/
private CharSequence buildOutput() {
if (template == null) {
return getString(R.string.msg_error_no_templates);
}
StringBuilder ret = new StringBuilder();
DateFormat df = DateFormat.getDateTimeInstance();
boolean alwaysGP = ((CheckBox) findViewById(R.id.always_gplay)).isChecked();
ListAdapter adapter = getListAdapter();
int count = adapter.getCount();
String now = java.text.DateFormat.getDateTimeInstance().format(
Calendar.getInstance().getTime());
int selected = 0;
for (int i = 0; i < count; i++) {
SortablePackageInfo spi = (SortablePackageInfo) adapter.getItem(i);
if (spi.selected) {
selected++;
String tmp = spi.installer;
if (alwaysGP) {
tmp = "com.google.vending";
}
String firstInstalled = df.format(new Date(spi.firstInstalled));
String lastUpdated = df.format(new Date(spi.lastUpdated));
String sourceLink = createSourceLink(tmp, spi.packageName);
String tmpl = template.item.replace("${comment}", noNull(spi.comment))
.replace("${tags}",noNull(spi.tags))
.replace("${packagename}", noNull(spi.packageName))
.replace("${displayname}", noNull(spi.displayName))
.replace("${source}", noNull(sourceLink))
.replace("${versioncode}", "" + spi.versionCode)
.replace("${targetsdk}", "" + spi.targetsdk)
.replace("${version}", noNull(spi.version))
.replace("${rating}", "" + spi.rating)
.replace("${uid}", "" + spi.uid)
.replace("${firstinstalled}", firstInstalled)
.replace("${lastupdated}", lastUpdated)
.replace("${datadir}", noNull(spi.dataDir))
.replace("${marketid}", noNull(spi.installer));
ret.append(tmpl);
}
}
ret.insert(
0,
template.header.replace("${now}", now).replace("${count}",
"" + selected));
ret.append(template.footer.replace("${now}", now).replace("${count}",
"" + selected));
return ret;
}