我有这个愚蠢的问题。 我搜索了它,但无法得到任何回复,因此我在这里发布了它。
在下面的片段中,我有一个recyclerview,它在 Gson 的帮助下用 Post POJO 填充来自服务器的数据。
我在我的另一个项目中多次使用类似的代码,但今天我收到一个错误,因为我的recyclerview没有显示值。所以我调试代码并得到以下输出,显示' this'我的回收视图在Volley的 onResponse 中无法使用。
My Fragment类如下:
public class Seller_Corner_Tab2 extends Fragment implements PostAdapter.OnItemClickListener {
public static final String TAG = Seller_Corner_Tab2.class.getSimpleName();
RecyclerView recyclerView;
ProgressDialog progressDialog;
AppPreferences preferences;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.seller_corner_tab2,container,false);
preferences = new AppPreferences(getActivity());
recyclerView = (RecyclerView)view.findViewById(R.id.seller_corner_2_rv);
LinearLayoutManager mLinearLayoutManagerVertical = new LinearLayoutManager(getContext()); // (Context context)
mLinearLayoutManagerVertical.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(mLinearLayoutManagerVertical);
progressDialog = new ProgressDialog(getContext());
/*progressDialog.setMessage("Loading your Posts");
progressDialog.setCancelable(false);
progressDialog.show();*/
fetchSellerPosts();
return view;
}
public void fetchSellerPosts() {
final Seller seller = EKrushiKattaApplication.getInstance().getPrefManager().getSeller();
StringRequest strReq = new StringRequest(Request.Method.POST,
EndPoints.GETPOSTBYID, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
ArrayList<Post>templist = new ArrayList<>();
/*progressDialog.hide();*/
try {
GsonBuilder gsonbuilder= new GsonBuilder();
Gson gson=gsonbuilder.create();
Log.e("Response",response);
templist =gson.fromJson(response, new TypeToken<List<Post>>(){}.getType());
}catch (Exception e)
{
e.printStackTrace();
}
recyclerView.setAdapter( new PostAdapter(templist,getContext(),Seller_Corner_Tab2.this));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.hide();
NetworkResponse networkResponse = error.networkResponse;
Log.e(TAG, "Connection error: " + error.getMessage() + ", code: " + networkResponse);
Toast.makeText(getActivity(), "Volley error: " + error.getMessage(), Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put(CommonLib.SELLER_ID,String.valueOf(seller.getSellerId()));
Log.e(TAG, "params: " + params.toString());
return params;
}
};
//Adding request to request queue
EKrushiKattaApplication.getInstance().addToRequestQueue(strReq);
}
@Override
public void onItemClick(Post post) {
}}
我的调试器显示了此输出
我的PostAdapter类如下:
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder>{
private ArrayList<Post> posts;
private Context context;
private OnItemClickListener listener;
public PostAdapter(ArrayList<Post> posts, Context context, OnItemClickListener listener) {
this.posts = posts;
this.context = context;
this.listener = listener;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_post, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
if(posts!=null){
final Post post = posts.get(position);
holder.title.setText(post.getPostTitle());
holder.desc.setText(post.getPostDescription());
holder.cost.setText(post.getPostCost());
RequestOptions options = new RequestOptions()
.centerCrop()
// .placeholder(R.drawable.placeholder)
// .error(R.drawable.ic_pic_error)
.priority(Priority.HIGH);
new GlideImageLoader(holder.image,
holder.progressBar).load(EndPoints.BASE_URL+post.getPostImage(),options);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onItemClick(post);
}
});
}
}
@Override
public int getItemCount() {
int size=0;
try {
size = posts.size();
} catch (Exception e) {
e.printStackTrace();
}
return size;
}
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
class ViewHolder extends RecyclerView.ViewHolder{
ImageView image;
TextView title,desc,cost;
ProgressBar progressBar;
public ViewHolder(View itemView) {
super(itemView);
image=(ImageView)itemView.findViewById(R.id.post_item_image);
title=(TextView)itemView.findViewById(R.id.post_item_title);
desc=(TextView)itemView.findViewById(R.id.post_item_desc);
cost=(TextView)itemView.findViewById(R.id.post_item_cost);
progressBar=(ProgressBar)itemView.findViewById(R.id.post_item_progressbar);
}
}
public interface OnItemClickListener {
void onItemClick(Post post);
}}
答案 0 :(得分:1)
试试这个:
1)从onResponse()
2)在回收者查看充气后立即创建适配器。
3)在onResponse()
填充新数据的数据源中。
4)在适配器中调用notifyDatasetChanged()
。
public class Seller_Corner_Tab2 extends Fragment implements PostAdapter.OnItemClickListener {
public static final String TAG = Seller_Corner_Tab2.class.getSimpleName();
RecyclerView recyclerView;
ProgressDialog progressDialog;
AppPreferences preferences;
ArrayList<Post>templist = new ArrayList<>();
PostAdapter adapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.seller_corner_tab2,container,false);
preferences = new AppPreferences(getActivity());
recyclerView = (RecyclerView)view.findViewById(R.id.seller_corner_2_rv);
LinearLayoutManager mLinearLayoutManagerVertical = new LinearLayoutManager(getContext()); // (Context context)
mLinearLayoutManagerVertical.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(mLinearLayoutManagerVertical);
progressDialog = new ProgressDialog(getContext());
adapter = new PostAdapter(templist,getContext(),this);
recyclerView.setAdapter(adapter);
/*progressDialog.setMessage("Loading your Posts");
progressDialog.setCancelable(false);
progressDialog.show();*/
fetchSellerPosts();
return view;
}
public void fetchSellerPosts() {
final Seller seller = EKrushiKattaApplication.getInstance().getPrefManager().getSeller();
StringRequest strReq = new StringRequest(Request.Method.POST,
EndPoints.GETPOSTBYID, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
ArrayList<Post> newList = new ArrayList();
/*progressDialog.hide();*/
try {
GsonBuilder gsonbuilder= new GsonBuilder();
Gson gson=gsonbuilder.create();
Log.e("Response",response);
templist =gson.fromJson(response, new TypeToken<List<Post>>(){}.getType());
}catch (Exception e)
{
e.printStackTrace();
}
templist.addAll(newList);
adapter.notifyDatasetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.hide();
NetworkResponse networkResponse = error.networkResponse;
Log.e(TAG, "Connection error: " + error.getMessage() + ", code: " + networkResponse);
Toast.makeText(getActivity(), "Volley error: " + error.getMessage(), Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put(CommonLib.SELLER_ID,String.valueOf(seller.getSellerId()));
Log.e(TAG, "params: " + params.toString());
return params;
}
};
//Adding request to request queue
EKrushiKattaApplication.getInstance().addToRequestQueue(strReq);
}
@Override
public void onItemClick(Post post) {
}}