Listview 数据不显示在recyclerview中,但在关闭和打开后显示数据列表
我正在开发一个应用程序,我正在使用volley库从服务器获取数据并显示在Listview 数据不显示在recyclerview中,但在关闭和打开后显示数据列表,listview,android-recyclerview,android-volley,android-adapter,Listview,Android Recyclerview,Android Volley,Android Adapter,我正在开发一个应用程序,我正在使用volley库从服务器获取数据并显示在RecyclerView中。。这里我使用了四个标签 问题在于,首先通过从服务器获取数据来运行要显示的元素列表,数据加载并添加到列表中,但RecyclerView从不显示它。当我关闭显示器并将其返回时,数据将列在RecyclerView 谁能帮帮我 下面是MyAdapter.java public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHol
RecyclerView
中。。这里我使用了四个标签
问题在于,首先通过从服务器获取数据来运行要显示的元素列表,数据加载并添加到列表中,但RecyclerView
从不显示它。当我关闭显示器并将其返回时,数据将列在RecyclerView
谁能帮帮我
下面是MyAdapter.java
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private ImageLoader mImageLoader;
private Context context;
ArrayList<Coupon> couponArrayList = new ArrayList<>();
public MyAdapter(Context context,ArrayList<Coupon> couponArrayList)
{
this.couponArrayList=couponArrayList;
this.context=context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.offer_row,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
viewHolder.relativeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView redUrl = (TextView)view.findViewById(R.id.offer_url);
String postUrl = redUrl.getText().toString();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(postUrl));
Intent browserChooserIntent = Intent.createChooser(intent , "Choose browser of your choice");
context.startActivity(browserChooserIntent);
}
});
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Coupon coupon = couponArrayList.get(position);
holder.title.setText(coupon.getTitle());
holder.name.setText(coupon.getName());
holder.coupon.setText(coupon.getCoupon());
holder.expiry.setText(coupon.getExpiry());
holder.url.setText(coupon.getLink());
//Image loading using singleton class
mImageLoader = MySingleton.getInstance(context).getImageLoader();
holder.image.setImageUrl(coupon.getImage(),mImageLoader);
holder.image.setDefaultImageResId(R.drawable.placeholder_image);
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
public void remove(ContactsContract.Contacts.Data data) {
int position = couponArrayList.indexOf(data);
couponArrayList.remove(position);
notifyItemRemoved(position);
}
@Override
public int getItemCount() {
return couponArrayList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder{
NetworkImageView image;
RelativeLayout relativeLayout;
TextView title,name,coupon,expiry,url;
public ViewHolder(View itemView) {
super(itemView);
this.image = (NetworkImageView)itemView.findViewById(R.id.offer_image);
this.title = (TextView)itemView.findViewById(R.id.offer_title);
this.name = (TextView)itemView.findViewById(R.id.offer_name);
this.coupon = (TextView)itemView.findViewById(R.id.coupon_code);
this.expiry = (TextView)itemView.findViewById(R.id.expiry_date);
this.url = (TextView)itemView.findViewById(R.id.offer_url);
this.relativeLayout = (RelativeLayout)itemView.findViewById(R.id.relLayout);
//make sure it is clickable
itemView.setClickable(true);
}
}
public class BackgroundTask {
Context context;
RequestQueue queue;
public static final String KEY_TITLE="coupon_title";
public static final String KEY_NAME="offer_name";
public static final String KEY_CODE="coupon_code";
public static final String KEY_IMAGE="store_image";
public static final String KEY_EXPIRY="coupon_expiry";
public static final String KEY_POSTURL="store_link";
private ImageLoader mImageLoader;
ArrayList<Coupon> arrayList = new ArrayList<>();
public static final String json_url="https://tools.vcommission.com/api/coupons.php?apikey=xxxxxxxxxx";
public BackgroundTask(Context context) {
this.context = context;
}
public ArrayList<Coupon> getList()
{
final JsonArrayRequest jsonArrayRequest=new JsonArrayRequest(Request.Method.POST, json_url,null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
int count=0;
while (count<response.length())
{
try {
JSONObject jsonObject= response.getJSONObject(count);
Coupon coupon = new Coupon(jsonObject.getString(KEY_TITLE),
jsonObject.getString(KEY_NAME),
jsonObject.getString(KEY_CODE),
jsonObject.getString(KEY_IMAGE),
jsonObject.getString(KEY_EXPIRY),
jsonObject.getString(KEY_POSTURL));
arrayList.add(coupon);
count++;
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context,"No internet connection...!!!",Toast.LENGTH_LONG).show();
error.printStackTrace();
}
});
MySingleton.getInstance(context).addToRequestQueue(jsonArrayRequest);
return arrayList;
}
}
最后是Backgroundtask.java
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private ImageLoader mImageLoader;
private Context context;
ArrayList<Coupon> couponArrayList = new ArrayList<>();
public MyAdapter(Context context,ArrayList<Coupon> couponArrayList)
{
this.couponArrayList=couponArrayList;
this.context=context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.offer_row,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
viewHolder.relativeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView redUrl = (TextView)view.findViewById(R.id.offer_url);
String postUrl = redUrl.getText().toString();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(postUrl));
Intent browserChooserIntent = Intent.createChooser(intent , "Choose browser of your choice");
context.startActivity(browserChooserIntent);
}
});
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Coupon coupon = couponArrayList.get(position);
holder.title.setText(coupon.getTitle());
holder.name.setText(coupon.getName());
holder.coupon.setText(coupon.getCoupon());
holder.expiry.setText(coupon.getExpiry());
holder.url.setText(coupon.getLink());
//Image loading using singleton class
mImageLoader = MySingleton.getInstance(context).getImageLoader();
holder.image.setImageUrl(coupon.getImage(),mImageLoader);
holder.image.setDefaultImageResId(R.drawable.placeholder_image);
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
public void remove(ContactsContract.Contacts.Data data) {
int position = couponArrayList.indexOf(data);
couponArrayList.remove(position);
notifyItemRemoved(position);
}
@Override
public int getItemCount() {
return couponArrayList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder{
NetworkImageView image;
RelativeLayout relativeLayout;
TextView title,name,coupon,expiry,url;
public ViewHolder(View itemView) {
super(itemView);
this.image = (NetworkImageView)itemView.findViewById(R.id.offer_image);
this.title = (TextView)itemView.findViewById(R.id.offer_title);
this.name = (TextView)itemView.findViewById(R.id.offer_name);
this.coupon = (TextView)itemView.findViewById(R.id.coupon_code);
this.expiry = (TextView)itemView.findViewById(R.id.expiry_date);
this.url = (TextView)itemView.findViewById(R.id.offer_url);
this.relativeLayout = (RelativeLayout)itemView.findViewById(R.id.relLayout);
//make sure it is clickable
itemView.setClickable(true);
}
}
public class BackgroundTask {
Context context;
RequestQueue queue;
public static final String KEY_TITLE="coupon_title";
public static final String KEY_NAME="offer_name";
public static final String KEY_CODE="coupon_code";
public static final String KEY_IMAGE="store_image";
public static final String KEY_EXPIRY="coupon_expiry";
public static final String KEY_POSTURL="store_link";
private ImageLoader mImageLoader;
ArrayList<Coupon> arrayList = new ArrayList<>();
public static final String json_url="https://tools.vcommission.com/api/coupons.php?apikey=xxxxxxxxxx";
public BackgroundTask(Context context) {
this.context = context;
}
public ArrayList<Coupon> getList()
{
final JsonArrayRequest jsonArrayRequest=new JsonArrayRequest(Request.Method.POST, json_url,null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
int count=0;
while (count<response.length())
{
try {
JSONObject jsonObject= response.getJSONObject(count);
Coupon coupon = new Coupon(jsonObject.getString(KEY_TITLE),
jsonObject.getString(KEY_NAME),
jsonObject.getString(KEY_CODE),
jsonObject.getString(KEY_IMAGE),
jsonObject.getString(KEY_EXPIRY),
jsonObject.getString(KEY_POSTURL));
arrayList.add(coupon);
count++;
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context,"No internet connection...!!!",Toast.LENGTH_LONG).show();
error.printStackTrace();
}
});
MySingleton.getInstance(context).addToRequestQueue(jsonArrayRequest);
return arrayList;
}
}
公共类背景任务{
语境;
请求队列;
公共静态最终字符串键\u TITLE=“优惠券\u TITLE”;
公共静态最终字符串键\u NAME=“offer\u NAME”;
公共静态最终字符串键\u CODE=“优惠券\u CODE”;
公共静态最终字符串KEY\u IMAGE=“store\u IMAGE”;
公共静态最终字符串键\u EXPIRY=“优惠券\u EXPIRY”;
公共静态最终字符串键\u postrl=“存储链接”;
私有图像加载器;
ArrayList ArrayList=新的ArrayList();
公共静态最终字符串json_url=”https://tools.vcommission.com/api/coupons.php?apikey=xxxxxxxxxx";
公共背景任务(上下文){
this.context=上下文;
}
公共ArrayList getList()
{
final JsonArrayRequest JsonArrayRequest=新的JsonArrayRequest(Request.Method.POST,json_url,null,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONArray响应){
整数计数=0;
而(count您的RecyclerView
第二次加载正确。因此适配器没有问题
问题是从服务器获得响应后,您没有在适配器上调用notifyDatasetChanged
。您还存在一些其他基本问题。我正在尝试稍微修改您的代码,以便您更容易理解问题
< >我只是从AlpOffice中添加必要的部分。java < /COD>。这里你可以考虑的是,在使用截击时保持同一个类的网络调用。当我们从服务器得到响应时,在<代码>适配器<代码>中更容易地调用<代码> NoTIFYDATABETHORKEX/<代码>。< /P>
public class AllOffers extends Fragment {
RequestQueue queue;
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
ArrayList<Coupon> arrayList = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.alloffers, container, false);
// Setup your RecyclerView here
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL));
// Set the adapter before the network call
adapter = new MyAdapter(getContext(), arrayList);
recyclerView.setAdapter(adapter);
// Move network call here
final JsonArrayRequest jsonArrayRequest=new JsonArrayRequest(Request.Method.POST, json_url,null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
int count=0;
while (count<response.length())
{
try {
JSONObject jsonObject= response.getJSONObject(count);
Coupon coupon = new Coupon(jsonObject.getString(KEY_TITLE),
jsonObject.getString(KEY_NAME),
jsonObject.getString(KEY_CODE),
jsonObject.getString(KEY_IMAGE),
jsonObject.getString(KEY_EXPIRY),
jsonObject.getString(KEY_POSTURL));
arrayList.add(coupon);
count++;
} catch (JSONException e) {
e.printStackTrace();
}
}
// Call notifyDatasetChanged on adapter so that the newly added items in the list takes effect in your RecyclerView
adapter.notifyDatasetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context,"No internet connection...!!!",Toast.LENGTH_LONG).show();
error.printStackTrace();
}
});
return view;
}
}
公共类alloffer扩展了片段{
请求队列;
回收视图回收视图;
RecyclerView.适配器;
RecyclerView.LayoutManager LayoutManager;
ArrayList ArrayList=新的ArrayList();
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=充气机。充气(右布局。alloffers,容器,假);
//在此处设置您的RecyclerView
recyclerView=(recyclerView)view.findViewById(R.id.recyclerView);
layoutManager=新的LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
addItemDecoration(新的DividerItemDecoration(getContext(),LinearLayoutManager.VERTICAL));
//在网络调用之前设置适配器
adapter=newmyadapter(getContext(),arrayList);
recyclerView.setAdapter(适配器);
//移动网络呼叫此处
final JsonArrayRequest JsonArrayRequest=新的JsonArrayRequest(Request.Method.POST,json_url,null,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONArray响应){
整数计数=0;
而(count您的RecyclerView
第二次加载正确。因此适配器没有问题
问题是从服务器获得响应后,您没有在适配器上调用notifyDatasetChanged
。您还存在一些其他基本问题。我正在尝试稍微修改您的代码,以便您更容易理解问题
< >我只是从AlpOffice中添加必要的部分。java < /COD>。这里你可以考虑的是,在使用截击时保持同一个类的网络调用。当我们从服务器得到响应时,在<代码>适配器<代码>中更容易地调用<代码> NoTIFYDATABETHORKEX/<代码>。< /P>
public class AllOffers extends Fragment {
RequestQueue queue;
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
ArrayList<Coupon> arrayList = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.alloffers, container, false);
// Setup your RecyclerView here
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL));
// Set the adapter before the network call
adapter = new MyAdapter(getContext(), arrayList);
recyclerView.setAdapter(adapter);
// Move network call here
final JsonArrayRequest jsonArrayRequest=new JsonArrayRequest(Request.Method.POST, json_url,null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
int count=0;
while (count<response.length())
{
try {
JSONObject jsonObject= response.getJSONObject(count);
Coupon coupon = new Coupon(jsonObject.getString(KEY_TITLE),
jsonObject.getString(KEY_NAME),
jsonObject.getString(KEY_CODE),
jsonObject.getString(KEY_IMAGE),
jsonObject.getString(KEY_EXPIRY),
jsonObject.getString(KEY_POSTURL));
arrayList.add(coupon);
count++;
} catch (JSONException e) {
e.printStackTrace();
}
}
// Call notifyDatasetChanged on adapter so that the newly added items in the list takes effect in your RecyclerView
adapter.notifyDatasetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context,"No internet connection...!!!",Toast.LENGTH_LONG).show();
error.printStackTrace();
}
});
return view;
}
}
公共类alloffer扩展了片段{
请求队列;
回收视图回收视图;
RecyclerView.适配器;
RecyclerView.LayoutManager LayoutManager;
ArrayList ArrayList=新的ArrayList();
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=充气机。充气(右布局。alloffers,容器,假);
//在此处设置您的RecyclerView
recyclerView=(recyclerView)view.findViewById(R.id.recyclerView);
layoutManager=新的LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
addItemDecoration(新的DividerItemDecoration(getContext(),LinearLayoutManager.VERTICAL));
//在网络调用之前设置适配器
adapter=newmyadapter(getContext(),arrayList);
recyclerView.setAdapter(适配器);
//移动网络呼叫此处
final JsonArrayRequest JsonArrayRequest=新的JsonArrayRequest(Request.Method.POST,json_url,null,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONArray响应){
整数计数=0;
而