android.widget.ListAdapter Java Examples

The following examples show how to use android.widget.ListAdapter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractListFragment.java    From android-viewer-for-khan-academy with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Build adapter, attach to data service, get appropriate cursor, and reset the list.
 * 
 * Cleanup in onDestroyView.
 */
@Override
public void onActivityCreated(Bundle savedInstanceState) {
	Log.v(LOG_TAG, ".onActivityCreated");
	super.onActivityCreated(savedInstanceState);
	
       ListAdapter adapter = buildListAdapter();
       setListAdapter(adapter);

	Activity a = getActivity();
	try {
		KADataService dataService = ((KADataService.Provider) a).getDataService();
		this.call(dataService);
		Log.d(LOG_TAG, "Service already available.");
	} catch (ServiceUnavailableException e) {
		boolean serviceExpected = ((KADataService.Provider) a).requestDataService(this);
		Log.d(LOG_TAG, String.format("Service expected: %b.", serviceExpected));
	}
	
	getListView().setOverScrollMode(ListView.OVER_SCROLL_ALWAYS);
}
 
Example #2
Source File: PinnedSectionListView.java    From Yahala-Messenger with MIT License 6 votes vote down vote up
@Override
public void setAdapter(ListAdapter adapter) {

    // assert adapter in debug mode
    if (BuildConfig.DEBUG && adapter != null) {
        if (!(adapter instanceof PinnedSectionListAdapter))
            throw new IllegalArgumentException("Does your adapter implement PinnedSectionListAdapter?");
        if (adapter.getViewTypeCount() < 2)
            throw new IllegalArgumentException("Does your adapter handle at least two types" +
                    " of views in getViewTypeCount() method: items and sections?");
    }

    // unregister observer at old adapter and register on new one
    ListAdapter oldAdapter = getAdapter();
    if (oldAdapter != null) oldAdapter.unregisterDataSetObserver(mDataSetObserver);
    if (adapter != null) adapter.registerDataSetObserver(mDataSetObserver);

    // destroy pinned shadow, if new adapter is not same as old one
    if (oldAdapter != adapter) destroyPinnedShadow();

    super.setAdapter(adapter);
}
 
Example #3
Source File: RecipientEditTextView.java    From ChipsLibrary with Apache License 2.0 6 votes vote down vote up
@Override
public <T extends ListAdapter & Filterable> void setAdapter(final T adapter)
  {
  super.setAdapter(adapter);
  ((BaseRecipientAdapter)adapter).registerUpdateObserver(new BaseRecipientAdapter.EntriesUpdatedObserver()
    {
      @Override
      public void onChanged(final List<RecipientEntry> entries)
        {
        // Scroll the chips field to the top of the screen so
        // that the user can see as many results as possible.
        if(entries!=null&&entries.size()>0)
          scrollBottomIntoView();
        }
    });
  }
 
Example #4
Source File: UltimateListview.java    From UltimateAndroid with Apache License 2.0 6 votes vote down vote up
public void setAdapter(ListAdapter adapter) {

//        mSwipeRefreshLayout.setRefreshing(false);
//        adapter.registerDataSetObserver(new DataSetObserver() {
//            @Override
//            public void onChanged() {
//                super.onChanged();
//                mSwipeRefreshLayout.setRefreshing(false);
//
//            }
//        });
        mBasicUltimateListView.setAdapter(adapter);
//        if ((adapter == null || adapter.getCount() == 0) && mEmptyId != 0) {
//            mEmpty.setVisibility(View.VISIBLE);
//        }
    }
 
Example #5
Source File: HeaderViewListAdapter.java    From PullToRefreshLibrary with Apache License 2.0 6 votes vote down vote up
public HeaderViewListAdapter(ArrayList<StaggeredGridView.FixedViewInfo> headerViewInfos,
                             ArrayList<StaggeredGridView.FixedViewInfo> footerViewInfos,
                             ListAdapter adapter) {
    mAdapter = adapter;
    mIsFilterable = adapter instanceof Filterable;

    if (headerViewInfos == null) {
        mHeaderViewInfos = EMPTY_INFO_LIST;
    } else {
        mHeaderViewInfos = headerViewInfos;
    }

    if (footerViewInfos == null) {
        mFooterViewInfos = EMPTY_INFO_LIST;
    } else {
        mFooterViewInfos = footerViewInfos;
    }

    mAreAllFixedViewsSelectable =
            areAllListInfosSelectable(mHeaderViewInfos)
                    && areAllListInfosSelectable(mFooterViewInfos);
}
 
Example #6
Source File: ViewUtils.java    From AnimeTaste with MIT License 6 votes vote down vote up
public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        return;
    }

    int totalHeight = 0;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
}
 
Example #7
Source File: ExtendableListView.java    From UltimateAndroid with Apache License 2.0 6 votes vote down vote up
public void run() {
    if (mDataChanged) return;

    final ListAdapter adapter = mAdapter;
    final int motionPosition = mClickMotionPosition;
    if (adapter != null && mItemCount > 0 &&
            motionPosition != INVALID_POSITION &&
            motionPosition < adapter.getCount() && sameWindow()) {
        final View view = getChildAt(motionPosition); // a fix by @pboos

        if (view != null) {
            final int clickPosition = motionPosition + mFirstPosition;
            performItemClick(view, clickPosition, adapter.getItemId(clickPosition));
        }
    }
}
 
Example #8
Source File: HeaderGridView.java    From AcDisplay with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Add a fixed view to appear at the top of the grid. If addHeaderView is
 * called more than once, the views will appear in the order they were
 * added. Views added using this call can take focus if they want.
 * <p/>
 * NOTE: Call this before calling setAdapter. This is so HeaderGridView can wrap
 * the supplied cursor with one that will also account for header views.
 *
 * @param v            The view to add.
 * @param data         Data to associate with this view
 * @param isSelectable whether the item is selectable
 */
public void addHeaderView(View v, Object data, boolean isSelectable) {
    ListAdapter adapter = getAdapter();
    if (adapter != null && !(adapter instanceof HeaderViewGridAdapter)) {
        throw new IllegalStateException(
                "Cannot add header view to grid -- setAdapter has already been called.");
    }
    FixedViewInfo info = new FixedViewInfo();
    FrameLayout fl = new FullWidthFixedViewLayout(getContext());
    fl.addView(v);
    info.view = v;
    info.viewContainer = fl;
    info.data = data;
    info.isSelectable = isSelectable;
    mHeaderViewInfos.add(info);
    // in the case of re-adding a header view, or adding one later on,
    // we need to notify the observer
    if (adapter != null) {
        ((HeaderViewGridAdapter) adapter).notifyDataSetChanged();
    }
}
 
Example #9
Source File: DragSortListView.java    From onpc with GNU General Public License v3.0 6 votes vote down vote up
AdapterWrapper(ListAdapter adapter)
{
    super();
    mAdapter = adapter;

    mAdapter.registerDataSetObserver(new DataSetObserver()
    {
        public void onChanged()
        {
            notifyDataSetChanged();
        }

        public void onInvalidated()
        {
            notifyDataSetInvalidated();
        }
    });
}
 
Example #10
Source File: PlaceDetailsActivity.java    From solutions-mobile-shopping-assistant-backend-java with Apache License 2.0 6 votes vote down vote up
/**
 * Updates UI to indicate that retrieval of the offers completed successfully or failed.
 */
@Override
protected void onPostExecute(CollectionResponseOffer result) {

  if (result == null || result.getItems() == null || result.getItems().size() < 1) {
    if (result == null) {
      offersListLabel.setText(R.string.failedToRetrieveOffers);
    } else {
      offersListLabel.setText(R.string.noOffers);
    }
    offersList.setAdapter(null);
    return;
  }

  offersListLabel.setText(R.string.offers);

  ListAdapter offersListAdapter = createOfferListAdapter(result.getItems());

  offersList.setAdapter(offersListAdapter);
}
 
Example #11
Source File: HeaderGridView.java    From AnimatedGridView with Apache License 2.0 6 votes vote down vote up
/**
 * Add a fixed view to appear at the top of the grid. If addHeaderView is
 * called more than once, the views will appear in the order they were
 * added. Views added using this call can take focus if they want.
 * <p>
 * NOTE: Call this before calling setAdapter. This is so HeaderGridView can wrap
 * the supplied cursor with one that will also account for header views.
 *
 * @param v The view to add.
 * @param data Data to associate with this view
 * @param isSelectable whether the item is selectable
 */
public void addHeaderView(View v, Object data, boolean isSelectable) {
    ListAdapter adapter = getAdapter();

    if (adapter != null && ! (adapter instanceof HeaderViewGridAdapter)) {
        throw new IllegalStateException(
                "Cannot add header view to grid -- setAdapter has already been called.");
    }

    FixedViewInfo info = new FixedViewInfo();
    FrameLayout fl = new FullWidthFixedViewLayout(getContext());
    fl.addView(v);
    info.view = v;
    info.viewContainer = fl;
    info.data = data;
    info.isSelectable = isSelectable;
    mHeaderViewInfos.add(info);

    // in the case of re-adding a header view, or adding one later on,
    // we need to notify the observer
    if (adapter != null) {
        ((HeaderViewGridAdapter) adapter).notifyDataSetChanged();
    }
}
 
Example #12
Source File: DragSortListView.java    From Overchan-Android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * For each DragSortListView Listener interface implemented by
 * <code>adapter</code>, this method calls the appropriate
 * set*Listener method with <code>adapter</code> as the argument.
 * 
 * @param adapter The ListAdapter providing data to back
 * DragSortListView.
 *
 * @see android.widget.ListView#setAdapter(android.widget.ListAdapter)
 */
@Override
public void setAdapter(ListAdapter adapter) {
    if (adapter != null) {
        mAdapterWrapper = new AdapterWrapper(adapter);
        adapter.registerDataSetObserver(mObserver);

        if (adapter instanceof DropListener) {
            setDropListener((DropListener) adapter);
        }
        if (adapter instanceof DragListener) {
            setDragListener((DragListener) adapter);
        }
        if (adapter instanceof RemoveListener) {
            setRemoveListener((RemoveListener) adapter);
        }
    } else {
        mAdapterWrapper = null;
    }

    super.setAdapter(mAdapterWrapper);
}
 
Example #13
Source File: PinnedSectionListView.java    From Contacts with MIT License 6 votes vote down vote up
int findCurrentSectionPosition(int fromPosition) {
	ListAdapter adapter = getAdapter();

	if (adapter instanceof SectionIndexer) {
		// try fast way by asking section indexer
		SectionIndexer indexer = (SectionIndexer) adapter;
		int sectionPosition = indexer.getSectionForPosition(fromPosition);
		int itemPosition = indexer.getPositionForSection(sectionPosition);
		int typeView = adapter.getItemViewType(itemPosition);
		if (isItemViewTypePinned(adapter, typeView)) {
			return itemPosition;
		} // else, no luck
	}

	// try slow way by looking through to the next section item above
	for (int position=fromPosition; position>=0; position--) {
		int viewType = adapter.getItemViewType(position);
		if (isItemViewTypePinned(adapter, viewType)) return position;
	}
	return -1; // no candidate found
}
 
Example #14
Source File: DragAndDropHandler.java    From ListViewAnimations with Apache License 2.0 6 votes vote down vote up
/**
 * @throws java.lang.IllegalStateException    if the adapter does not have stable ids.
 * @throws java.lang.IllegalArgumentException if the adapter does not implement {@link com.nhaarman.listviewanimations.util.Swappable}.
 */
private void setAdapterInternal(@NonNull final ListAdapter adapter) {
    ListAdapter actualAdapter = adapter;
    if (actualAdapter instanceof WrapperListAdapter) {
        actualAdapter = ((WrapperListAdapter) actualAdapter).getWrappedAdapter();
    }

    if (!actualAdapter.hasStableIds()) {
        throw new IllegalStateException("Adapter doesn't have stable ids! Make sure your adapter has stable ids, and override hasStableIds() to return true.");
    }

    if (!(actualAdapter instanceof Swappable)) {
        throw new IllegalArgumentException("Adapter should implement Swappable!");
    }

    mAdapter = actualAdapter;
}
 
Example #15
Source File: BlacklistActivity.java    From HeadsUp with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void setListAdapter(ListAdapter adapter) {
    if (adapter == null) {
        super.setListAdapter(null);
    } else {
        List<Header> headers = null;
        try {
            Method method = PreferenceActivity.class.getDeclaredMethod("getHeaders");
            method.setAccessible(true);
            headers = (List<Header>) method.invoke(this);
        } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
            e.printStackTrace();
        }

        super.setListAdapter(new HeaderAdapter(this, headers));
    }
}
 
Example #16
Source File: HorizontalListView.java    From sealtalk-android with MIT License 6 votes vote down vote up
@Override
public void setAdapter(ListAdapter adapter) {
    if (mAdapter != null) {
        mAdapter.unregisterDataSetObserver(mAdapterDataObserver);
    }

    if (adapter != null) {
        // Clear so we can notify again as we run out of data
        mHasNotifiedRunningLowOnData = false;

        mAdapter = adapter;
        mAdapter.registerDataSetObserver(mAdapterDataObserver);
    }

    initializeRecycledViewCache(mAdapter.getViewTypeCount());
    reset();
}
 
Example #17
Source File: AbstractListActivityAssert.java    From assertj-android with Apache License 2.0 5 votes vote down vote up
public S hasListAdapter(ListAdapter adapter) {
  isNotNull();
  ListAdapter actualAdapter = actual.getListAdapter();
  assertThat(actualAdapter) //
      .overridingErrorMessage("Expected list adapter <%s> but was <%s>.", adapter,
          actualAdapter) //
      .isSameAs(adapter);
  return myself;
}
 
Example #18
Source File: IcsSpinner.java    From CSipSimple with GNU General Public License v3.0 5 votes vote down vote up
/**
 * If the wrapped SpinnerAdapter is also a ListAdapter, delegate this call.
 * Otherwise, return true.
 */
public boolean isEnabled(int position) {
    final ListAdapter adapter = mListAdapter;
    if (adapter != null) {
        return adapter.isEnabled(position);
    } else {
        return true;
    }
}
 
Example #19
Source File: SelectItemQuizView.java    From android-topeka with Apache License 2.0 5 votes vote down vote up
@Override
public void setUserInput(Bundle savedInput) {
    if (savedInput == null) {
        return;
    }
    mAnswers = savedInput.getBooleanArray(KEY_ANSWERS);
    if (mAnswers == null) {
        return;
    }
    final ListAdapter adapter = mListView.getAdapter();
    for (int i = 0; i < mAnswers.length; i++) {
        mListView.performItemClick(mListView.getChildAt(i), i, adapter.getItemId(i));
    }
}
 
Example #20
Source File: PinnedSectionListView.java    From AndroidWeekly with Apache License 2.0 5 votes vote down vote up
void recreatePinnedShadow() {
    destroyPinnedShadow();
    ListAdapter adapter = getAdapter();
    if (adapter != null && adapter.getCount() > 0) {
        int firstVisiblePosition = getFirstVisiblePosition();
        int sectionPosition = findCurrentSectionPosition(firstVisiblePosition);
        if (sectionPosition == -1) return; // no views to pin, exit
        ensureShadowForPosition(sectionPosition,
                firstVisiblePosition, getLastVisiblePosition() - firstVisiblePosition);
    }
}
 
Example #21
Source File: ListViewActivity.java    From GlassActionBar with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ListAdapter adapter = new ImagesAdapter(this);
    helper = new GlassActionBarHelper().contentLayout(R.layout.activity_listview, adapter);
    setContentView(helper.createView(this));
}
 
Example #22
Source File: Utils.java    From Musicoco with Apache License 2.0 5 votes vote down vote up
/**
 * 获得 ListView 的高度,每一项的高度之和
 */
public static int getListViewHeight(@NonNull ListView listView) {
    int totalHeight = 0;
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter != null) {
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
    }
    return totalHeight;
}
 
Example #23
Source File: ListPopupWindow.java    From material with Apache License 2.0 5 votes vote down vote up
/**
 * Perform an item click operation on the specified list adapter position.
 *
 * @param position Adapter position for performing the click
 * @return true if the click action could be performed, false if not.
 *         (e.g. if the popup was not showing, this method would return false.)
 */
public boolean performItemClick(int position) {
    if (isShowing()) {
        if (mItemClickListener != null) {
            final DropDownListView list = mDropDownList;
            final View child = list.getChildAt(position - list.getFirstVisiblePosition());
            final ListAdapter adapter = list.getAdapter();
            mItemClickListener.onItemClick(list, child, position, adapter.getItemId(position));
        }
        return true;
    }
    return false;
}
 
Example #24
Source File: MergeAdapter.java    From Shield with MIT License 5 votes vote down vote up
/**
 * Returns the number of types of Views that will be created by getView().
 */
@Override
public int getViewTypeCount() {
    int total = 0;

    for (ListAdapter piece : pieces) {
        total += piece.getViewTypeCount();
    }

    return (Math.max(total, 1)); // needed for setListAdapter() before
    // content add'
}
 
Example #25
Source File: MergeAdapter.java    From mimicry with Apache License 2.0 5 votes vote down vote up
/**
 * Get the adapter associated with the specified position in the data set.
 * @param position Position of the item whose adapter we want
 */
public ListAdapter getAdapter(int position) {
	for (ListAdapter piece : pieces) {
		int size = piece.getCount();

		if (position < size) {
			return (piece);
		}

		position -= size;
	}

	return (null);
}
 
Example #26
Source File: HListView.java    From Klyph with MIT License 5 votes vote down vote up
/**
 * Find a position that can be selected (i.e., is not a separator).
 * 
 * @param position
 *           The starting position to look at.
 * @param lookDown
 *           Whether to look down for other positions.
 * @return The next selectable position starting at position and then searching either up or down. Returns
 *         {@link #INVALID_POSITION} if nothing can be found.
 */
@Override
protected int lookForSelectablePosition( int position, boolean lookDown ) {
	final ListAdapter adapter = mAdapter;
	if ( adapter == null || isInTouchMode() ) {
		return INVALID_POSITION;
	}

	final int count = adapter.getCount();
	if ( !mAreAllItemsSelectable ) {
		if ( lookDown ) {
			position = Math.max( 0, position );
			while ( position < count && !adapter.isEnabled( position ) ) {
				position++;
			}
		} else {
			position = Math.min( position, count - 1 );
			while ( position >= 0 && !adapter.isEnabled( position ) ) {
				position--;
			}
		}

		if ( position < 0 || position >= count ) {
			return INVALID_POSITION;
		}
		return position;
	} else {
		if ( position < 0 || position >= count ) {
			return INVALID_POSITION;
		}
		return position;
	}
}
 
Example #27
Source File: ScrollerListActivity.java    From Virtualview-Android with MIT License 5 votes vote down vote up
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(android.R.layout.list_content);
    List<Map<String, String>> list = new ArrayList<Map<String, String>>();
    HashMap<String, String> scroll0 = new HashMap<String, String>();
    scroll0.put("name", "ScrollerVL");
    scroll0.put("desp", "orientation = V | mode = linear");
    scroll0.put("class", ComponentActivity.class.getName());
    scroll0.put("data", "component_demo/slider_item.json");
    list.add(scroll0);
    HashMap<String, String> scroll1 = new HashMap<String, String>();
    scroll1.put("name", "ScrollerVS");
    scroll1.put("desp", "orientation = V | mode = staggered");
    scroll1.put("class", ComponentActivity.class.getName());
    scroll1.put("data", "component_demo/slider_item.json");
    list.add(scroll1);
    HashMap<String, String> scroll2 = new HashMap<String, String>();
    scroll2.put("name", "ScrollerH");
    scroll2.put("desp", "orientation = H");
    scroll2.put("class", ComponentActivity.class.getName());
    scroll2.put("data", "component_demo/slider_item.json");
    list.add(scroll2);
    ListAdapter listAdapter = new SimpleAdapter(this, list, android.R.layout.simple_list_item_2,
        new String[] {"name", "desp"}, new int[] {android.R.id.text1, android.R.id.text2});
    setListAdapter(listAdapter);
}
 
Example #28
Source File: PinnedSectionListView.java    From Yahala-Messenger with MIT License 5 votes vote down vote up
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

    if (mDelegateOnScrollListener != null) { // delegate
        mDelegateOnScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
    }

    // get expected adapter or fail fast
    ListAdapter adapter = getAdapter();
    if (adapter == null || visibleItemCount == 0) return; // nothing to do

    final boolean isFirstVisibleItemSection =
            isItemViewTypePinned(adapter, adapter.getItemViewType(firstVisibleItem));

    if (isFirstVisibleItemSection) {
        View sectionView = getChildAt(0);
        if (sectionView.getTop() == getPaddingTop()) { // view sticks to the top, no need for pinned shadow
            destroyPinnedShadow();
        } else { // section doesn't stick to the top, make sure we have a pinned shadow
            ensureShadowForPosition(firstVisibleItem, firstVisibleItem, visibleItemCount);
        }

    } else { // section is not at the first visible position
        int sectionPosition = findCurrentSectionPosition(firstVisibleItem);
        if (sectionPosition > -1) { // we have section position
            ensureShadowForPosition(sectionPosition, firstVisibleItem, visibleItemCount);
        } else { // there is no section for the first visible item, destroy shadow
            destroyPinnedShadow();
        }
    }
}
 
Example #29
Source File: MergeAdapter.java    From mimicry with Apache License 2.0 5 votes vote down vote up
public final int getSectionForPosition(int position) {
	int section = 0;

	for (ListAdapter piece : pieces) {
		int size = piece.getCount();

		if (position < size) {
			if (piece instanceof SectionIndexer) {
				return (section + ((SectionIndexer) piece).getSectionForPosition(position));
			}

			return (0);
		} else {
			if (piece instanceof SectionIndexer) {
				Object[] sections = ((SectionIndexer) piece).getSections();

				if (sections != null) {
					section += sections.length;
				}
			}
		}

		position -= size;
	}

	return (0);
}
 
Example #30
Source File: PinnedSectionListView.java    From KUAS-AP-Material with MIT License 5 votes vote down vote up
int findCurrentSectionPosition(int fromPosition) {
	ListAdapter adapter = getAdapter();

	// dataset has changed, no candidate
	if (fromPosition >= adapter.getCount()) {
		return -1;
	}

	if (adapter instanceof SectionIndexer) {
		// try fast way by asking section indexer
		SectionIndexer indexer = (SectionIndexer) adapter;
		int sectionPosition = indexer.getSectionForPosition(fromPosition);
		int itemPosition = indexer.getPositionForSection(sectionPosition);
		int typeView = adapter.getItemViewType(itemPosition);
		if (isItemViewTypePinned(adapter, typeView)) {
			return itemPosition;
		} // else, no luck
	}

	// try slow way by looking through to the next section item above
	for (int position = fromPosition; position >= 0; position--) {
		int viewType = adapter.getItemViewType(position);
		if (isItemViewTypePinned(adapter, viewType)) {
			return position;
		}
	}
	// no candidate found
	return -1;
}