我有一个从数据库填充数据的recyclerview。处理单击事件后,会增加特定行单击的次数,对notifyItemChanged(position)和notifyDatasetChanged的调用对视图没有影响。请问我做错了什么
这是我的代码
适配器
public class ExaminationAdapter extends RecyclerView.Adapter<ExaminationAdapter.EAViewHolder> {
private Cursor mCursor;
private static ExaminationClickListener clickListener;
public interface ExaminationClickListener {
void onClick(View v, int rowID, int position, boolean longClick);
}
public ExaminationAdapter (ExaminationClickListener clickListener){
this.clickListener = clickListener;
}
@Override
public EAViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// inflate the android.R.layout.simple_list_item_1 layout
View view = LayoutInflater.from(parent.getContext()).inflate(
R.layout.recyclerview_examination, parent, false);
return new EAViewHolder(view); // return current item's ViewHolder
}
@Override
public void onBindViewHolder(EAViewHolder holder, int position) {
mCursor.moveToPosition(position);
holder.setRowID(mCursor.getLong(mCursor.getColumnIndex(SinEntry._ID)));
holder.mTextView1.setText(mCursor.getString(mCursor.getColumnIndex(SinEntry.COLUMN_DESCRIPTION)));
int count = mCursor.getInt(mCursor.getColumnIndex(ConfessionContract.PersonToSinEntry.COLUMN_COUNT));
holder.mTextView2.setText(String.valueOf(count));
holder.setPosition(position);
/*try {
}
catch (Resources.NotFoundException e) {
holder.mTextView2.setText("0");
}*/
}
@Override
public int getItemCount() { return (mCursor != null) ? mCursor.getCount() : 0; }
// swap this adapter's current Cursor for a new one
public void swapCursor(Cursor cursor) {
mCursor = cursor;
notifyDataSetChanged();
}
public class EAViewHolder extends RecyclerView.ViewHolder {
public final TextView mTextView1;
public final TextView mTextView2;
private long rowID;
private int position;
public EAViewHolder(View itemView) {
super(itemView);
mTextView1 = (TextView) itemView.findViewById(R.id.txtSin);
mTextView2 = (TextView) itemView.findViewById(R.id.txtSinCount);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickListener.onClick(v, (int) rowID, position, false);
}
});
itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
clickListener.onClick(v, (int) rowID, position, true);
return true;
}
});
}
// set the database row ID for the examination in this ViewHolder
public void setRowID(long rowID) {
this.rowID = rowID;
}
public void setPosition (int position ) {
this.position = position;
}
}
}
片段
public class ExaminationFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final int EXAMINATION_LOADER = 2;// identifies Loader
public static final String PREFS_NAME = "MyPrefsFile";
private RecyclerView mRecyclerView;
private String selection;
private String[] selectionArgs;
private int id;
// TODO: Rename and change types of parameters
private int commandmentID;
private String mParam2;
private OnFragmentInteractionListener mListener;
private ExaminationAdapter examinationAdapter;
private static final String[] EXAMINATION_COLUMNS = {
ConfessionContract.SinEntry.TABLE_NAME + "." + ConfessionContract.SinEntry._ID,
ConfessionContract.SinEntry.TABLE_NAME + "." + ConfessionContract.SinEntry.COLUMN_DESCRIPTION,
ConfessionContract.PersonToSinEntry.TABLE_NAME + "." + ConfessionContract.PersonToSinEntry.COLUMN_PERSON_ID,
ConfessionContract.PersonToSinEntry.TABLE_NAME + "." + ConfessionContract.PersonToSinEntry.COLUMN_COUNT
};
private static final String[] COUNT_COLUMNS ={
ConfessionContract.PersonToSinEntry.COLUMN_COUNT
};
public ExaminationFragment() {
// Required empty public constructor
}
public static ExaminationFragment newInstance(String param1, String param2) {
ExaminationFragment fragment = new ExaminationFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences user = getContext().getSharedPreferences(PREFS_NAME, 0);
int vocation = user.getInt("vocation", 99);
long dob = user.getLong("birthDate", 99);
int sex = user.getInt("sex", 99);
id = user.getInt("id", 99);
if (getArguments() != null) {
commandmentID = getArguments().getInt(ExaminationActivity.COMMANDMENT_URI);
}
selection = "(" +ConfessionContract.SinEntry.COLUMN_COMMANDMENT_ID + " = ? AND " +
Utility.calculateAgeBracket(dob) + " AND " +
Utility.vocationSelection(vocation)+ " AND " +
Utility.sexSelection(sex) + " AND " +
ConfessionContract.PersonToSinEntry.COLUMN_PERSON_ID + " = ?" + ")"
+ " OR " +"(" +ConfessionContract.SinEntry.COLUMN_COMMANDMENT_ID + " = ? AND " +
Utility.calculateAgeBracket(dob) + " AND " +
Utility.vocationSelection(vocation)+ " AND " +
Utility.sexSelection(sex) + " AND " +
ConfessionContract.PersonToSinEntry.COLUMN_PERSON_ID + " is NULL" + ")";
selectionArgs = new String[]{String.valueOf(commandmentID), String.valueOf(id), String.valueOf(commandmentID) };
getLoaderManager().initLoader(EXAMINATION_LOADER, null, this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_examination, container, false);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerViewE);
mRecyclerView.setHasFixedSize(true);
examinationAdapter = new ExaminationAdapter(new ExaminationAdapter.ExaminationClickListener() {
@Override
public void onClick(View v, int rowID, int position, boolean longClick) {
if (longClick) {
Toast.makeText(getContext(), rowID + " long click", Toast.LENGTH_LONG).show();
}
else {
updateCount(rowID, position);
Toast.makeText(getContext(), String.valueOf(rowID), Toast.LENGTH_LONG).show();
}
}
});
mRecyclerView.setAdapter(examinationAdapter); // set the adapter
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return rootView;
}
private void updateCount(int rowID, int position) {
int count = 0;
String selection = ConfessionContract.PersonToSinEntry.COLUMN_PERSON_ID + " = ?"
+ " AND " + ConfessionContract.PersonToSinEntry.COLUMN_SINS_ID + " = ?";
String[] selectionArgs ={String.valueOf(id), String.valueOf(rowID)};
Cursor cursor = getActivity().getContentResolver().query(ConfessionContract.PersonToSinEntry.CONTENT_URI,
COUNT_COLUMNS,
selection,
selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()){
count = cursor.getInt(cursor.getColumnIndex(ConfessionContract.PersonToSinEntry.COLUMN_COUNT));
}
if (cursor != null) {
cursor.close();
}
ContentValues contentValues = new ContentValues();
contentValues.put(ConfessionContract.PersonToSinEntry.COLUMN_PERSON_ID, id);
contentValues.put(ConfessionContract.PersonToSinEntry.COLUMN_SINS_ID, rowID);
contentValues.put(ConfessionContract.PersonToSinEntry.COLUMN_COUNT, count + 1);
int sinUri = getActivity().getContentResolver().update(
ConfessionContract.PersonToSinEntry.buildSinUri(rowID),
contentValues,
selection,
selectionArgs);
**mRecyclerView.getAdapter().notifyItemChanged(position);**
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
CursorLoader cursorLoader;
String sortOrder = ConfessionContract.SinEntry._ID + " COLLATE NOCASE ASC";
cursorLoader = new CursorLoader(getActivity(),
ConfessionContract.SinEntry.CONTENT_URI, // Uri of sins table
EXAMINATION_COLUMNS, // null projection returns all columns
selection, // null selection returns all rows
selectionArgs, // no selection arguments
sortOrder); // sort order
return cursorLoader;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
examinationAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
examinationAdapter.swapCursor(null);
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}