问题描述
我收到来自服务器的数据,然后分析它并将其存储在一个列表。我使用这个列表中RecyclerView的适配器。我使用的片段。
I am getting data from server and then parsing it and storing it in a List. I am using this list for the RecyclerView's adapter. I am using Fragments.
我正在使用的Nexus 5与奇巧。我使用的支持库这一点。这是否会有所作为?
I am using a Nexus 5 with KitKat. I am using support library for this. Will this make a difference?
下面是我的code:(使用虚拟数据的问题)
Here is my code: (Using dummy data for the question)
成员变量:
List<Business> mBusinesses = new ArrayList<Business>();
RecyclerView recyclerView;
RecyclerView.LayoutManager mLayoutManager;
BusinessAdapter mBusinessAdapter;
我的 onCreateView()
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Getting data from server
getBusinessesDataFromServer();
View view = inflater.inflate(R.layout.fragment_business_list,
container, false);
recyclerView = (RecyclerView) view
.findViewById(R.id.business_recycler_view);
recyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
mBusinessAdapter = new BusinessAdapter(mBusinesses);
recyclerView.setAdapter(mBusinessAdapter);
return view;
}
从服务器获取数据后, parseResponse()
之称。
protected void parseResponse(JSONArray response, String url) {
// insert dummy data for demo
mBusinesses.clear();
Business business;
business = new Business();
business.setName("Google");
business.setDescription("Google HeadQuaters");
mBusinesses.add(business);
business = new Business();
business.setName("Yahoo");
business.setDescription("Yahoo HeadQuaters");
mBusinesses.add(business);
business = new Business();
business.setName("Microsoft");
business.setDescription("Microsoft HeadQuaters");
mBusinesses.add(business);
Log.d(Const.DEBUG, "Dummy Data Inserted\nBusinesses Length: "
+ mBusinesses.size());
mBusinessAdapter = new BusinessAdapter(mBusinesses);
mBusinessAdapter.notifyDataSetChanged();
}
我的BusinessAdapter:
My BusinessAdapter:
public class BusinessAdapter extends
RecyclerView.Adapter<BusinessAdapter.ViewHolder> {
private List<Business> mBusinesses = new ArrayList<Business>();
// Provide a reference to the type of views that you are using
// (custom viewholder)
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextViewName;
public TextView mTextViewDescription;
public ImageView mImageViewLogo;
public ViewHolder(View v) {
super(v);
mTextViewName = (TextView) v
.findViewById(R.id.textView_company_name);
mTextViewDescription = (TextView) v
.findViewById(R.id.textView_company_description);
mImageViewLogo = (ImageView) v
.findViewById(R.id.imageView_company_logo);
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public BusinessAdapter(List<Business> myBusinesses) {
Log.d(Const.DEBUG, "BusinessAdapter -> constructor");
mBusinesses = myBusinesses;
}
// Create new views (invoked by the layout manager)
@Override
public BusinessAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
Log.d(Const.DEBUG, "BusinessAdapter -> onCreateViewHolder()");
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(
R.layout.item_business_list, parent, false);
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, "BusinessAdapter -> onBindViewHolder()");
Business item = mBusinesses.get(position);
holder.mTextViewName.setText(item.getName());
holder.mTextViewDescription.setText(item.getDescription());
holder.mImageViewLogo.setImageResource(R.drawable.ic_launcher);
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
Log.d(Const.DEBUG, "BusinessAdapter -> getItemCount()");
if (mBusinesses != null) {
Log.d(Const.DEBUG, "mBusinesses Count: " + mBusinesses.size());
return mBusinesses.size();
}
return 0;
}
}
但我不明白,在视图中显示的数据。我究竟做错了什么?
But I don't get the data displayed in the view. What am I doing wrong?
下面是我的日志,
07-14 21:15:35.669: D/xxx(2259): Dummy Data Inserted
07-14 21:15:35.669: D/xxx(2259): Businesses Length: 3
07-14 21:26:26.969: D/xxx(2732): BusinessAdapter -> constructor
我不明白这之后的任何日志。不应该 getItemCount()
适配器应该再次调用?
I don't get any logs after this. Shouldn't getItemCount()
in adapter should be called again?
推荐答案
在你的 parseResponse()
正在创建的新实例的 BusinessAdapter
类,但实际上并没有使用任何地方,所以你的 RecyclerView
不知道新的实例存在。
In your parseResponse()
you are creating a new instance of the BusinessAdapter
class, but you aren't actually using it anywhere, so your RecyclerView
doesn't know the new instance exists.
您要么需要:
- 呼叫
recyclerView.setAdapter(mBusinessAdapter)
再次更新RecyclerView的适配器引用指向新的 - 或者直接删除
mBusinessAdapter =新BusinessAdapter(mBusinesses);
继续使用现有的适配器。既然你没有更改mBusinesses
参考,适配器将仍然使用数组列表,当你调用应正确更新notifyDataSetChanged()
。
- Call
recyclerView.setAdapter(mBusinessAdapter)
again to update the RecyclerView's adapter reference to point to your new one - Or just remove
mBusinessAdapter = new BusinessAdapter(mBusinesses);
to continue using the existing adapter. Since you haven't changed themBusinesses
reference, the adapter will still use that array list and should update correctly when you callnotifyDataSetChanged()
.
这篇关于notifyDataSetChanged不工作的RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!