我知道notifyDataSetChanged()适用于RecyclerView,因为我之前使用它并且它没有任何问题。但在我当前的应用程序中,notifyDataSetChanged()不会更新我的RecyclerView。如果有人能够发现我的代码中的逻辑缺陷,那将会很高兴。
private List<ItemPetitionsTable> mAlVerified = new ArrayList<ItemPetitionsTable>();
private ViewPagerPetitionsRecyclerViewAdapter mViewPagerPetitionsRecyclerViewAdapter;
private RecyclerView mRecyclerView;
private StaggeredGridLayoutManager mStaggeredLayoutManager;
onCreateView()中的:
mAlVerified.clear();
mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerview_verified_petitions);
mRecyclerView.setHasFixedSize(true);
mStaggeredLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(mStaggeredLayoutManager);
mViewPagerPetitionsRecyclerViewAdapter = new ViewPagerPetitionsRecyclerViewAdapter(activity, mAlVerified);
mRecyclerView.setAdapter(mViewPagerPetitionsRecyclerViewAdapter);
然后网络操作继续从服务器获取数据...并存储在本地数据库中。调用getDataFromDatabase()...
private void getDataFromDatabase() {
if (Const.DEBUGGING)
Log.d(Const.DEBUG, "getDataFromDatabase()");
if (mSwipeRefreshLayout.isRefreshing())
mSwipeRefreshLayout.setRefreshing(false);
mAlVerified = mPetitionsTableDbAdapter.getPetitionsForType(DatabaseHelper.PETITION_TYPE_ALL);
Log.d(Const.DEBUG, "List Size: " + mAlVerified.size());
mViewPagerPetitionsRecyclerViewAdapter.notifyDataSetChanged();
isRefreshing = false;
}
Logcat显示该列表包含新数据:但不会刷新recyclelerview。
50.018 21055-21055/com.xx.xxx D/xxx: getDataFromDatabase()
03-09 19:09:50.019 21055-21055/com.xx.xxx D/xxx: List Size: 10
如果您还有其他需要,请告诉我。
使用compile 'com.android.support:recyclerview-v7:23.1.1'
如果这可能有帮助
public class ViewPagerPetitionsRecyclerViewAdapter extends RecyclerView.Adapter<ViewPagerPetitionsRecyclerViewAdapter.ViewHolder>
implements Filterable {
private List<ItemPetitionsTable> mItems = new ArrayList<>();
private Context context;
private SearchFilter searchFilter;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public ImageView mImageView;
public ViewHolder(View v) {
super(v);
mTextView = (TextView) v.findViewById(R.id.textView_description);
mImageView = (ImageView) v.findViewById(R.id.imageView_main_image);
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public ViewPagerPetitionsRecyclerViewAdapter(Context context, List<ItemPetitionsTable> items) {
this.context = context;
this.mItems = items;
Log.d(Const.DEBUG, "mItems Size: " + items.size());
}
// Create new views (invoked by the layout manager)
@Override
public ViewPagerPetitionsRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_viewpager_fragment_petitions, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
Log.d(Const.DEBUG, "onBindViewHolder(): " + position);
String url = "http://xxx.org.in/Attachment/e-petition_img.jpg";
if (mItems.get(position).getAttachments() == null
|| mItems.get(position).getAttachments().equalsIgnoreCase("")) {
Picasso.with(context).load(url).into(holder.mImageView);
} else {
try {
JSONArray array = new JSONArray(mItems.get(position).getAttachments());
if (array.length() == 0)
url = "http://xxx.org.in/Attachment/e-petition_img.jpg";
else {
for (int i = 0; i < array.length(); i++) {
JSONObject attachment_object = array.getJSONObject(i);
String type = attachment_object.getString("typ");
if (type.equalsIgnoreCase("i")) {
url = attachment_object.getString("Doc_path");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
Picasso.with(context).load(url).into(holder.mImageView);
}
Typeface face = Typeface.createFromAsset(context.getAssets(),
"fonts/RobotoSlab-Regular.ttf");
holder.mTextView.setTypeface(face);
holder.mTextView.setText(mItems.get(position).getPetition_title());
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mItems.size();
}
@Override
public Filter getFilter() {
if (searchFilter == null)
searchFilter = new SearchFilter();
return searchFilter;
}
private class SearchFilter extends Filter {
private PetitionsTableDbAdapter mPetitionsTableDbAdapter = DatabaseHelper.get(context).getPetitionsTableDbAdapter();
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
ArrayList<ItemPetitionsTable> searchResults = new ArrayList<ItemPetitionsTable>();
for (ItemPetitionsTable item : mItems) {
if (item.getPetition_number().toLowerCase().contains(constraint.toString().toLowerCase())) {
searchResults.add(item);
}
}
results.count = searchResults.size();
results.values = searchResults;
} else {
mItems = (ArrayList) mPetitionsTableDbAdapter.getPetitionsForType(DatabaseHelper.PETITION_TYPE_ALL);
results.count = mItems.size();
results.values = mItems;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
mItems = (List<ItemPetitionsTable>) results.values;
notifyDataSetChanged();
}
}
答案 0 :(得分:1)
在适配器中再创建一个方法,例如ViewPagerPetitionsRecyclerViewAdapter。使用该方法更新适配器中的日期,然后调用notifyDataSetChanged()
示例代码
private void getDataFromDatabase() {
if (Const.DEBUGGING)
Log.d(Const.DEBUG, "getDataFromDatabase()");
if (mSwipeRefreshLayout.isRefreshing())
mSwipeRefreshLayout.setRefreshing(false);
mAlVerified = mPetitionsTableDbAdapter.getPetitionsForType(DatabaseHelper.PETITION_TYPE_ALL);
Log.d(Const.DEBUG, "List Size: " + mAlVerified.size());
// u need to update data here
mViewPagerPetitionsRecyclerViewAdapter.updatedata(mAlVerified);
mViewPagerPetitionsRecyclerViewAdapter.notifyDataSetChanged();
isRefreshing = false;
}