为什么我的recyclerview没有显示任何内容?

mctunoxg  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(385)

我有个大问题。我的 RecyclerView 不工作,我也不´我不知道如何解决我的问题。在我的日志中,碎片似乎没有调用 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;
}
}

我的回收适配器

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}

当我开始它不显示回收查看是什么问题,试图解决了10个小时了。

q35jwt9p

q35jwt9p1#

您正在将空数据添加到 RecyclerView 当你打电话的时候 recyclerAdapter.addData(listdata)AmbilData() 方法,因为您在 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在评论中指出的,你必须检查 URL

ssm49v7z

ssm49v7z2#

我解决了我的问题。我在getitemcount方法中得到了大小为null的值,因为我的php代码是错误的。我没有抛出正确的变量。
我的代码:`

?php foreach ($events AS $event):
         $ID=json_encode($events);
         print($ID);
endforeach; ?>

在我使用$event之前,现在我使用$events作为参数。
`

相关问题