将JSON数据从服务器保存到回收者视图仅显示最后一个JSON项目

时间:2018-09-09 13:46:56

标签: php android json android-recyclerview

我正在尝试从服务器中获取数据并将其存储在回收者视图中。代码正确无误。该应用程序运行,但仅显示最后一个JSON项目。我不知道我的代码有什么问题。以下是我的活动代码:

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);

}

}

此代码用于我的适配器类文件:

public 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);

        }
    }

我找不到问题出在哪里,一切对我来说看起来都很不错,当我运行此活动时,仍然只有一个项目可见。

2 个答案:

答案 0 :(得分:2)

这些行应在onCreate()中调用,并在listItems = new ArrayList<>();之后调用

adapter = new Adapter(listItems,getApplicationContext());
recyclerView.setAdapter(adapter);

并在这样的适配器内部创建函数

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);

答案 1 :(得分:1)

您必须在adapter.notifyDataSetChanged();之后致电recyclerView.setAdapter(adapter);