为啥我的 RecyclerView 不显示任何内容?
Posted
技术标签:
【中文标题】为啥我的 RecyclerView 不显示任何内容?【英文标题】:Why my RecyclerView doesnt show anything?为什么我的 RecyclerView 不显示任何内容? 【发布时间】:2021-04-08 03:16:01 【问题描述】:我有一个大问题。
我的RecyclerView
不起作用,我不知道如何解决我的问题。
在我的 Logcat 中,Fragment 似乎不调用onResponse
,而只调用ErrorResponse
。
我尝试了一切,但没有任何帮助。
不知道哪一部分出错了,为什么不起作用。
我的家庭片段:
public class Home_Fragment extends Fragment
private String URL= "http://localhost/pages/index.php";
private RecyclerAdapter recyclerAdapter;
private RecyclerView recyclerView;
private ArrayList<Data> listdata;
private static final String TAG = "Home_Fragment";
private GridLayoutManager gridLayoutManager;
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
public Home_Fragment()
// Required empty public constructor
private void AmbilData()
JsonArrayRequest aarRequest = new JsonArrayRequest(URL,
new Response.Listener<JSONArray>()
@Override
public void onResponse(JSONArray response)
if (response.length()>0)
for (int i =0 ; i<response.length();i++)
try
JSONObject data = response.getJSONObject(i);
Data item = new Data();
item.setEventname(data.getString("Eventname"));
item.setEventhost(data.getString("Eventclub"));
item.setEventclub(data.getString("Eventhost"));
item.setGenre(data.getString("Genre"));
item.setDate(data.getString("Date"));
item.setAdress(data.getString("Adress"));
item.setTime(data.getString("Time"));
item.setPrice(data.getString("Price"));
catch (JSONException e)
recyclerAdapter.addData(listdata);
,
new Response.ErrorListener()
@Override
public void onErrorResponse(VolleyError error)
)
;
Volley.newRequestQueue(getActivity()).add(aarRequest);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
Log.d(TAG, "onCreateView: createview");
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home_, container, false);
AmbilData();
recyclerView = view.findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
gridLayoutManager = new GridLayoutManager(getActivity(), 2);
gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(gridLayoutManager);
listdata = new ArrayList<Data>();
recyclerAdapter = new RecyclerAdapter(getActivity(),listdata);
recyclerView.setAdapter(recyclerAdapter);
recyclerAdapter.notifyDataSetChanged();
Log.d(TAG, "onCreateView: viewreturned");
return view;
我的 RecyclerAdapter
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>
private ArrayList<Data> listdata;
private Activity activity;
private Context context;
private static final String TAG = "Recycler";
public RecyclerAdapter(Activity activity, ArrayList<Data> listdata)
this.listdata = listdata;
this.activity = activity;
@Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
Log.d(TAG, "onCreateViewHolder: create Holder");
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.event_cardview, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
@Override
public void onBindViewHolder(ViewHolder holder, int position)
Log.d(TAG, "onBindViewHolder: Create holder Items");
//holder.mImage.setImageResource(listdata.get(position).getThubnail());
holder.eventname.setText(listdata.get(position).getEventname());
holder.eventclub.setText(listdata.get(position).getEventclub());
holder.eventhost.setText(listdata.get(position).getEventhost());
holder.genre.setText(listdata.get(position).getGenre());
holder.date.setText(listdata.get(position).getDate());
holder.adress.setText(listdata.get(position).getAdress());
holder.time.setText(listdata.get(position).getTime());
holder.price.setText(listdata.get(position).getPrice());
public void addData(List<Data> data)
listdata.addAll(data);
notifyDataSetChanged();
@Override
public int getItemCount()
Log.d(TAG, "getItemCount: return size");
return listdata.size();
public static class ViewHolder extends RecyclerView.ViewHolder
private CardView cv;
private TextView eventname,eventclub,eventhost,genre,date,adress,time,price;
private static final String TAG = "Class";
public ViewHolder(View v)
super(v);
Log.d(TAG, "ViewHolder: created");
cv=(CardView)v.findViewById(R.id.eventcardview);
eventname=(TextView)v.findViewById(R.id.EventName);
eventclub=(TextView)v.findViewById(R.id.EventClub);
eventhost=(TextView)v.findViewById(R.id.EventHost);
genre=(TextView)v.findViewById(R.id.Genre);
date=(TextView)v.findViewById(R.id.dark);
adress=(TextView)v.findViewById(R.id.location_adress);
time=(TextView)v.findViewById(R.id.Time);
price=(TextView)v.findViewById(R.id.Price);
我的数据
public class Data
private String eventname;
private String eventclub;
private String eventhost;
private String genre;
private String date;
private String adress;
private String time;
private String price;
public String getEventname()
return eventname;
public void setEventname(String eventname)
this.eventname = eventname;
public String getEventclub()
return eventclub;
public void setEventclub(String eventlcub)
this.eventclub = eventclub;
public String getEventhost()
return eventhost;
public void setEventhost(String eventhost)
this.eventhost = eventhost;
public String getGenre()
return genre;
public void setGenre(String genre)
this.genre = genre;
public String getDate()
return date;
public void setDate(String date)
this.date = date;
public String getAdress()
return adress;
public void setAdress(String adress)
this.adress = adress;
public String getTime()
return time;
public void setTime(String time)
this.time = time;
public String getPrice()
return price;
public void setPrice(String price)
this.price = price;
当我在我的计算机上打开 localhost/pages/index.php 时,它会显示:
"ID":1,"Eventname":"Imagine-Event","Eventclub":"Imagine-Club","Eventhost":"Imagine-Host","Genre":"Techno","Date":"2020-12-31","Time":"22:00-06:00","Adress":"Frankfurt Gutleutstra\u00dfe 126a","Price":15
"ID":2,"Eventname":"Hello-Event","Eventclub":"Hello-Club","Eventhost":"Hello-Host","Genre":"Hello","Date":"2021-01-01","Time":"21:00-6:00","Adress":"Frankfurt am Main Sachsenhausengasse 128a","Price":5
"ID":3,"Eventname":"Fantasy-Event","Eventclub":"Fantasy-Club","Eventhost":"Fantasy-Host","Genre":"Mainstream","Date":"2021-02-19","Time":"00:00-12:00","Adress":"Langen (Hessen) Sehretstra\u00dfe 33a","Price":5
"ID":4,"Eventname":"This Event","Eventclub":"This-Club","Eventhost":"This Host","Genre":"Hip-Hop","Date":"2021-03-03","Time":"20:00-2:00","Adress":"Frankfurt am Main Goethestra\u00dfe","Price":15
当我启动它时,它不显示 Recyclerview 是什么问题试图解决它 10 个小时。
【问题讨论】:
onError 返回什么错误?里面有关于这个问题的数据。 其实我可以告诉你——网址是错误的。本地主机将是电话本身,而不是运行服务器的 PC。即使它是模拟器,模拟器也会运行虚拟机。您需要使用服务器的实际 IP(或 DNS 名称,但您可能没有)。 感谢您的回答我更改了 URL 但它现在不起作用似乎运行时 if 行未调用添加了一些 LOG 并且只调用了 ErrorResponse LOG。 【参考方案1】:当您在AmbilData()
方法中调用recyclerAdapter.addData(listdata)
时,您将空数据添加到RecyclerView
适配器,因为您遍历了JSON
列表并且没有将项目添加到listdata
数据列表中。
要解决这个问题,请在循环中使用listdata.add(item)
。
private void AmbilData()
JsonArrayRequest aarRequest = new JsonArrayRequest(URL,
new Response.Listener<JSONArray>()
@Override
public void onResponse(JSONArray response)
if (response.length()>0)
for (int i =0 ; i<response.length();i++)
try
JSONObject data = response.getJSONObject(i);
Data item = new Data();
item.setEventname(data.getString("Eventname"));
item.setEventhost(data.getString("Eventclub"));
item.setEventclub(data.getString("Eventhost"));
item.setGenre(data.getString("Genre"));
item.setDate(data.getString("Date"));
item.setAdress(data.getString("Adress"));
item.setTime(data.getString("Time"));
item.setPrice(data.getString("Price"));
listdata.add(item); // <<<<<< Here is the Change
catch (JSONException e)
recyclerAdapter.addData(listdata);
...
正如@Gabe Sechan 在 cmets 中指出的那样,您必须检查URL
【讨论】:
感谢您的回答,我更改了网址并添加了您的线路。不幸的是,它没有解决我的问题,在 if 行之前添加了一些 LOG,在 if 行内添加了一些。似乎它不运行 if 行并在不使用 if 行的情况下跳转到 ErrorResponse。不知道当我在手机上使用该 URL 时也可能出现问题,但在模拟器上它在应用程序的 Homefragment 内没有显示任何内容 我在 getCount 方法中添加了一个新的 LOG,以查看是否计算了某些内容,但它只给我一个 0 似乎我的适配器没有得到任何数据。【参考方案2】:我解决了我的问题。 我在 getItemcount 方法中得到了 size null ,因为我的 PHP 代码是错误的。 我没有抛出正确的变量。
我的代码: `
?php foreach ($events AS $event):
$ID=json_encode($events);
print($ID);
endforeach; ?>
在我使用 $event 之前,我现在使用 $events 作为参数。
`
【讨论】:
以上是关于为啥我的 RecyclerView 不显示任何内容?的主要内容,如果未能解决你的问题,请参考以下文章