我已经使用光标寻呼机适配器实现了视图寻呼机,因为它从SQLite
以随机顺序提取数据,并在寻呼机的片段中显示相同的数据。它工作得非常好。现在我想给用户一个选项,将这些引号标记为收藏。我的方法是在数据库中设置一个反对quote_id的标志,并且已经成功实现了这个,但问题是每当我向右或向左滑动并且想要不喜欢最喜欢的那个人时。引用然后它不起作用,因为刷卡上的ID不正确,因为如果我是第三个引用,那么第一个引用的ID将被显示。需要你帮助解决这个问题。
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;
}