我有对象数组。当我需要通过单个vaue过滤对象时,我这样做:
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
public class ItemPagerFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
/**
* The pager widget, which handles animation and allows swiping horizontally to access previous
* and next wizard steps.
*/
private ViewPager mPager;
/**
* The pager adapter, which provides the pages to the view pager widget.
*/
private CursorPagerAdapter adapter;
private ImageButton favButton;
private ImageButton shareButton;
public ItemPagerFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
System.gc();
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_item_pager, container, false);
// Instantiate a ViewPager and a PagerAdapter in app.
mPager = (ViewPager) rootView.findViewById(R.id.pager);
getLoaderManager().initLoader(-5, null, this);
mPager.setOffscreenPageLimit(0);
adapter = new CursorPagerAdapter(getFragmentManager(), null);
mPager.setAdapter(adapter);
mPager.setOnPageChangeListener(new OnPageChangeListener() {
// This method will be invoked when a new page becomes selected.
@Override
public void onPageSelected(int position) {
//Toast.makeText(getActivity(),
//"Selected page position: " + ++position, Toast.LENGTH_SHORT).show();
}
// This method will be invoked when the current page is scrolled
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// Code goes here
}
// Called when the scroll state changes:
// SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
@Override
public void onPageScrollStateChanged(int state) {
// Code goes here
}
});
return rootView;
}
@Override
public void onDestroyView() {
System.gc();
super.onDestroyView();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
CursorLoader loader = new CursorLoader(getActivity(),
DataProvider.CONTENT_URI_DATA,
null,
null, // "favorite = 0"
null,
"RANDOM()");
return loader;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
adapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
adapter.swapCursor(null);
}
/**
* A simple pager adapter.
*/
private class CursorPagerAdapter extends FragmentStatePagerAdapter {
//HashMap<Integer,ItemDetailFragment> hash = new HashMap<Integer,ItemDetailFragment>();
ItemDetailFragment[] record = new ItemDetailFragment[3];
//HashMap<Integer,ItemDetailFragment> hash = new HashMap<Integer,ItemDetailFragment>(3);
private Cursor mCursor;
public CursorPagerAdapter(FragmentManager fm, Cursor c) {
super(fm);
mCursor = c;
}
@Override
public Fragment getItem(int position) {
if (mCursor.moveToPosition(position)) {
Bundle arguments = new Bundle();
arguments.putLong(ItemDetailFragment.ARG_ITEM_ID, mCursor.getLong(mCursor.getColumnIndex(DataProvider.COL_ID)));
ItemDetailFragment fragment = new ItemDetailFragment();
fragment.setArguments(arguments);
// System.out.println("putting page " + position + " in " + position % 3);
record[position%3] = fragment;
//hash.put(position , fragment);
return fragment;
}
return null;
}
@Override
public int getCount() {
if (mCursor != null) {
return mCursor.getCount();
}
return 0;
}
public void swapCursor(Cursor cursor) {
mCursor = cursor;
try {
notifyDataSetChanged();
}
catch(Exception e)
{
System.out.println("exception caught");
}
}
public void destroyItem(ViewGroup container, int position, Object object){
record[position%3] = null;
super.destroyItem(container,position,object);
}
private ItemDetailFragment getfragment(int position)
{
return record[position%3];
//return hash.get(position);
}
public int getItemPosition(Object object) {
ItemDetailFragment f = (ItemDetailFragment ) object;
int position = mPager.getCurrentItem();
ItemDetailFragment current = adapter.getfragment(position);
if(current.equals(f))
{
int fav = Integer.parseInt(f.data.getFavorite());
if(fav == 1)
f.update(1);
else
f.update(0);
}
return super.getItemPosition(object);
}
//changes to be done here
/*public int getItemPosition(Object object) {
ItemDetailFragment f = (ItemDetailFragment ) object;
int position = mPager.getCurrentItem();
System.out.println("prerna update "+position);
ItemDetailFragment current = adapter.getfragment(position);
if(current.equals(f))
{
System.out.println("prerna got it");
int fav = Integer.parseInt(f.data.getFavorite());
if(fav == 1)
f.update(1);
else
f.update(0);
}
return super.getItemPosition(object);
// return POSITION_NONE;//comment afterwards
}*/
}
}
但是作为一种选择,我想通过&#34; All&#34;来过滤2&#39; Phase`值对象。或&#34; Home&#34;在这种情况下如何过滤?
我试过这样:
$scope.filteredByPhase = $filter('filter')($scope.allApps, {Phase:"All"});
$scope.allAppsBatch = $scope.filteredByPhase;
但是没有用......任何人都指导我吗?
答案 0 :(得分:2)
在AngularJS中,您可以将函数用作过滤器中的表达式。在该函数中,您可以验证条件并返回布尔值。从结果中筛选出所有falsy
项。所以你可以做到
$scope.filteredByPhase = $filter('filter')($scope.allApps, function (app) {
if (app.Phase == "All" || app.Phase == "Home") {
return true;
}
return false;
});
答案 1 :(得分:0)
使用$ filter传递匿名比较函数。
$scope.filteredItems = $filter('filter')($scope.items, function (item) {
return (item.Phase == "all") ? true : false;
});
请注意,您也可以使用Array.filter:
$scope.items = [{
Phase: "home"
}, {
Phase: "all"
}, {
Phase: "all"
}, {
Phase: "home"
}];
console.log($scope.items);
$scope.filteredItems = $scope.items.filter(function (item) {
return (item.Phase == "all") ? true : false;
})
console.log($scope.filteredItems)
您还可以使用链接触发多个过滤操作:
$scope.fi = $scope.i.filter(func1).filter(func2);