Php 将JSON数据从服务器保存到回收器视图仅显示最后一个JSON项
我正在尝试从服务器获取数据并将其存储在recycler视图中。代码正确无误。应用程序运行,但只显示最后一个JSON项。我不知道我的代码出了什么问题。以下是我的活动代码:Php 将JSON数据从服务器保存到回收器视图仅显示最后一个JSON项,php,android,json,android-recyclerview,Php,Android,Json,Android Recyclerview,我正在尝试从服务器获取数据并将其存储在recycler视图中。代码正确无误。应用程序运行,但只显示最后一个JSON项。我不知道我的代码出了什么问题。以下是我的活动代码: public class Under8ClubSchedule extends AppCompatActivity { public ProgressBar progressBar; private static final String URL="http://prasaurus.com/conn_u8_club_sched
public class Under8ClubSchedule extends AppCompatActivity {
public ProgressBar progressBar;
private static final String URL="http://prasaurus.com/conn_u8_club_schedule.php";
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<Schedule_Items> listItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_under8_club_schedule);
progressBar = (ProgressBar)findViewById(R.id.pb);
recyclerView = (RecyclerView)findViewById(R.id.u8_club_schedule);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
listItems = new ArrayList<>();
adapter = new Adapter(listItems,getApplicationContext());
recyclerView.setAdapter(adapter);
loadRecyclerViewData();
}
private void loadRecyclerViewData(){
progressBar.setVisibility(View.VISIBLE);
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
int success = jsonObject.getInt("success");
if(success == 1) {
JSONArray jsonArray = jsonObject.getJSONArray("under8_club_schedule");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject_current = jsonArray.getJSONObject(i);
Schedule_Items schedule_items = new Schedule_Items(
jsonObject_current.getString("team_one"),
jsonObject_current.getString("team_two"),
jsonObject_current.getString("score"),
jsonObject_current.getString("league_name"),
jsonObject_current.getString("date"),
jsonObject_current.getString("time"),
jsonObject_current.getString("location")
);
listItems.add(schedule_items);
}
}
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
progressBar.setVisibility(View.GONE);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG);
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
8ClubSchedule下的公共类扩展了AppCompative活动{
公共ProgressBar ProgressBar;
私有静态最终字符串URL=”http://prasaurus.com/conn_u8_club_schedule.php";
私人回收站;
专用循环视图适配器;
私人物品清单;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(8俱乐部计划下的右布局活动);
progressBar=(progressBar)findViewById(R.id.pb);
recyclerView=(recyclerView)findViewById(R.id.u8俱乐部时间表);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(新的LinearLayoutManager(本));
listItems=new ArrayList();
adapter=新适配器(listItems,getApplicationContext());
recyclerView.setAdapter(适配器);
loadRecycleServiceWdata();
}
私有void loadRecyclerViewData(){
progressBar.setVisibility(View.VISIBLE);
StringRequest StringRequest=新建StringRequest(Request.Method.GET,URL,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
试一试{
JSONObject JSONObject=新JSONObject(响应);
int success=jsonObject.getInt(“success”);
如果(成功==1){
JSONArray JSONArray=jsonObject.getJSONArray(“8俱乐部计划下”);
for(int i=0;ipublic class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
private List<Schedule_Items> listItems;
private Context context;
public Adapter(List<Schedule_Items> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.schedule_orientation,parent,false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Schedule_Items schedule_items = listItems.get(position);
holder.textView_team_name1.setText(schedule_items.getTeam_name1());
holder.textView_team_name2.setText(schedule_items.getTeam_name2());
holder.textView_score.setText(schedule_items.getScore());
holder.textView_league_name.setText(schedule_items.getLeague_name());
holder.textView_date.setText(schedule_items.getDate());
holder.textView_time.setText(schedule_items.getTime());
holder.textView_location.setText(schedule_items.getLocation());
}
@Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView textView_team_name1;
public TextView textView_team_name2;
public TextView textView_score;
public TextView textView_league_name;
public TextView textView_date;
public TextView textView_time;
public TextView textView_location;
public ViewHolder(@NonNull View itemView) {
super(itemView);
textView_team_name1 = (TextView)itemView.findViewById(R.id.team_name1);
textView_team_name2 = (TextView)itemView.findViewById(R.id.team_name2);
textView_score = (TextView)itemView.findViewById(R.id.score);
textView_league_name = (TextView)itemView.findViewById(R.id.league_name);
textView_date = (TextView)itemView.findViewById(R.id.date);
textView_time = (TextView)itemView.findViewById(R.id.time);
textView_location = (TextView)itemView.findViewById(R.id.location);
}
}
公共类适配器扩展了RecyclerView.Adapter{
私人物品清单;
私人语境;
公共适配器(列表项、上下文){
this.listItems=listItems;
this.context=上下文;
}
@非空
@凌驾
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType){
视图v=LayoutInflater.from(parent.getContext()).flate(R.layout.schedule\u方向,parent,false);
返回新的视图持有者(v);
}
@凌驾
public void onBindViewHolder(@NonNull ViewHolder,int位置){
Schedule\u Items Schedule\u Items=listItems.get(位置);
holder.textView\u team\u name1.setText(schedule\u items.getTeam\u name1());
holder.textView\u team\u name2.setText(schedule\u items.getTeam\u name2());
holder.textView_score.setText(schedule_items.getScore());
holder.textView\u league\u name.setText(schedule\u items.getLeague\u name());
holder.textView_date.setText(schedule_items.getDate());
holder.textView\u time.setText(schedule\u items.getTime());
holder.textView_location.setText(schedule_items.getLocation());
}
@凌驾
public int getItemCount(){
返回listItems.size();
}
公共类ViewHolder扩展了RecyclerView.ViewHolder{
公共文本视图文本视图团队名称1;
公共文本视图文本视图团队名称2;
公共文本视图文本视图_分数;
公共文本视图文本视图联盟名称;
公共文本视图文本视图_日期;
公共文本查看文本查看时间;
公共文本视图文本视图位置;
公共视图持有者(@NonNull View itemView){
超级(项目视图);
textView\u team\u name1=(textView)itemView.findViewById(R.id.team\u name1);
textView\u team\u name2=(textView)itemView.findViewById(R.id.team\u name2);
textView_score=(textView)itemView.findViewById(R.id.score);
textView\u league\u name=(textView)itemView.findViewById(R.id.league\u name);
textView_date=(textView)itemView.findViewById(R.id.date);
textView_time=(textView)itemView.findViewById(R.id.time);
textView_location=(textView)itemView.findViewById(R.id.location);
}
}
我找不到哪里出了问题,在我看来一切都很好,但当我运行此活动时,只有一项可见。您必须在
recyclerView.setAdapter(适配器)之后调用adapter.notifyDataSetChanged();
您必须在recyclerView.setAdapter(适配器)之后调用adapter.notifyDataSetChanged();
这些行应该在onCreate()内部调用,并在listItems=new ArrayList()之后调用;
并在适配器中创建一个函数,如下所示
public void setData(Arraylist<Schedule_Items> items){
this.listItems = items;
notifyDataSetChanged();
}
public void setData(数组列表项){
this.listItems=项目;
notifyDataSetChanged();
}
public void setData(Arraylist<Schedule_Items> items){
this.listItems = items;
notifyDataSetChanged();
}
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject_current = jsonArray.getJSONObject(i);
Schedule_Items schedule_items = new Schedule_Items(
jsonObject_current.getString("team_one"),
jsonObject_current.getString("team_two"),
jsonObject_current.getString("score"),
jsonObject_current.getString("league_name"),
jsonObject_current.getString("date"),
jsonObject_current.getString("time"),
jsonObject_current.getString("location")
);
listItems.add(schedule_items);
}
}
adapter.setData(listItems);