dimanche 24 mai 2015

Unable to capture random id in view pager

I have implemented view pager with cursor pager adapter as in it is fetching the data in random order from SQLite and displaying the same in pager's fragment. It is working absolutely fine. Now I want to give user an option to mark these quotes as favourite. My approach is to set a flag against quote_id in database and have achieved this too successfully but the problem is whenever i swipe right or left and wants to unfavorite the 'favorite one' quote then it is not working because id on swipe is incorrect as in if i am 3rd quote then id of 1st quote is getting displayed. Need your help in resolving the same.

ItemPagerFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // 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);
    adapter = new CursorPagerAdapter(getFragmentManager(), null);
    mPager.setAdapter(adapter);
    return rootView;
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {

    CursorLoader loader = new CursorLoader(getActivity(), 
            DataProvider.CONTENT_URI_DATA, 
            null, 
            null, 
            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 {

    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);
            return fragment;
        }
        return null;
    }

    @Override
    public int getCount() {
        if (mCursor != null) {
            return mCursor.getCount();
        }
        return 0;
    }

    public void swapCursor(Cursor cursor) {
        mCursor = cursor;
        notifyDataSetChanged();

    }
}    

}

ItemDetailFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getArguments().containsKey(ARG_ITEM_ID)) {
        id = getArguments().getLong(ARG_ITEM_ID);

}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_item_detail, container, false);
    FrameLayout rlB =(FrameLayout)rootView.findViewById(R.id.layout);

        Uri contentUri = ContentUris.withAppendedId(DataProvider.CONTENT_URI_DATA, id);
        Cursor c = getActivity().getContentResolver().query(contentUri, null, null, null, "RANDOM()");

        if (c != null) {
                if (c.moveToFirst()) {
                final int favFlag = c.getInt(c.getColumnIndex(DataProvider.COL_FAVFLAG));
                final ImageButton favButton = (ImageButton) rootView.findViewById(R.id.buttonFav);
            if(favFlag ==1)
            {
                favButton.setBackgroundResource(R.drawable.addtofav1);
            }
                else
                {   
                    favButton.setBackgroundResource(R.drawable.addtofav);
                }
                favButton.setOnClickListener(new Button.OnClickListener(){

                       @Override
                       public void onClick(View v) {
                           if(favFlag == 0)
                           {
                               favButton.setBackgroundResource(R.drawable.addtofav1);
                               Uri contentUri = ContentUris.withAppendedId(DataProvider.CONTENT_URI_DATA, id);
                                ContentValues cv = new ContentValues();
                                cv.put("favorite", "1");
                                int count = getActivity().getContentResolver().update(contentUri, cv, null,null);
                                Toast.makeText(getActivity(),
                                        "Added To Favorite " + count,
                                        Toast.LENGTH_SHORT).show();

                           }
                           else
                           {
                               favButton.setBackgroundResource(R.drawable.addtofav);
                                  //update the object  
                               Uri contentUri = ContentUris.withAppendedId(DataProvider.CONTENT_URI_DATA, id);
                                ContentValues cv = new ContentValues();
                                cv.put("favorite", "0");
                                int count = getActivity().getContentResolver().update(contentUri, cv, null,null);
                                Toast.makeText(getActivity(),
                                        "Removed From Favorite" + count,
                                        Toast.LENGTH_SHORT).show();

                           }
                       }}); 
            }
            c.close();
        }
    }

    return rootView;
}

Aucun commentaire:

Enregistrer un commentaire