当前位置: 代码迷 >> java >> Java Arraylist被填充但没有更新?
  详细解决方案

Java Arraylist被填充但没有更新?

热度:40   发布时间:2023-07-26 14:20:40.0

我正在尝试制作一个可以获取实时游戏成绩的应用程序,但是在使用recycleviewer和arraylist时遇到了麻烦。

RecycleView可以正常工作,但是当我制作要填充RecycleView的arraylist时出现了麻烦。 在这里,我只用100个游戏对象填充数组,这可行。 我得到的屏幕上充满了100个随机游戏。

private void updateUI() {

    ArrayList<Games> games = new ArrayList<>();

    for (int i = 0; i < 100; i++) {
        Games g = new Games("t1",110);
        g.setTeam2("T2");
        g.setTeam2_score(120);
        games.add(g);
    }
    mAdapter = new GameAdapter(games);
    mGameRecycleViewer.setAdapter(mAdapter);
}

我不想要任何随机的100个对象,我想获取一天发生的游戏并用它们填充数组。 因此,我使用JSON来获取游戏,并且由于该日志消息,我知道数组中充满了游戏:Log.d(“ AAAAAHHHHHHH”,Integer.toString(games.size())); 告诉我得到此输出的数组大小:

D/AAAAAHHHHHHH: 1
D/AAAAAHHHHHHH: 2
D/AAAAAHHHHHHH: 2
 D/AAAAAHHHHHHH: 3
D/AAAAAHHHHHHH: 3
D/AAAAAHHHHHHH: 4
 D/AAAAAHHHHHHH: 4
 D/AAAAAHHHHHHH: 5
D/AAAAAHHHHHHH: 5
D/AAAAAHHHHHHH: 6
D/AAAAAHHHHHHH: 6
D/AAAAAHHHHHHH: 7
D/AAAAAHHHHHHH: 7
D/AAAAAHHHHHHH: 8
D/AAAAAHHHHHHH: 8
D/AAAAAHHHHHHH: 9
D/AAAAAHHHHHHH: 9
D/AAAAAHHHHHHH: 10
D/AAAAAHHHHHHH: 10
D/AAAAAHHHHHHH: 11
 D/AAAAAHHHHHHH: 11

另外,我尝试使用JSON而不使用回收站查看器,而是在屏幕上输出所有内容,因此也可以使用。 问题是,尽管上面显示了原因,但由于某种原因,没有任何东西留在游戏数组中。 它不会显示在屏幕上。

我还在执行后立即添加了另一个logd,它说大小为0:Log.d(“ OOOHHHHHHH”,Integer.toString(games.size()));

因此由于某种原因,数组无法在运行类之外填充。 任何人有任何建议,或知道如何修复?

这是使用JSON的updateUI:

ArrayList<Games> games = new ArrayList<>();

private void updateUI() {
    class run extends AsyncTask<Void,Void,Void> {
        String line = "";
        String data = "";
        @Override
        protected Void doInBackground(Void... voids) {

            try {

                URL url = new URL("https://stats.nba.com/stats/scoreboard/?GameDate=02/13/2019&LeagueID=00&DayOffset=0");

                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                InputStream is = conn.getInputStream();
                BufferedReader bf = new BufferedReader(new InputStreamReader(is));

                while(line!=null){
                    line = bf.readLine();
                    data += line;
                }
                JSONObject jo = new JSONObject(data);

                JSONArray ja = (JSONArray) jo.get("resultSets");
                JSONObject jo2 = (JSONObject) ja.get(1);//Linescores
                JSONArray ja3 = (JSONArray) jo2.get("rowSet");
                JSONArray ja4;

                int count = 0;
                for(int i = 0;i<ja3.length();i++){
                    ja4 = new JSONArray(ja3.get(i).toString());
                    //teams+=ja4.get(4)+": score =  "+ja4.get(21).toString()+"\n";
                    if(count == 1) {
                        games.get(games.size() - 1).setTeam2(ja4.get(4).toString());
                        games.get(games.size() - 1).setTeam2_score(Integer.parseInt(ja4.get(21).toString()));
                        count = 0;
                    }else{
                        games.add(new Games(ja4.get(4).toString(), Integer.parseInt(ja4.get(21).toString())));
                        count = 1;
                    }
                    Log.d("AAAAAHHHHHHH",Integer.toString(games.size()));
                }




            }catch (Exception e){
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            //main.setText(teams);

        }
    }
    run r = new run();

    r.execute();
    mAdapter = new GameAdapter(games);
    mGameRecycleViewer.setAdapter(mAdapter);
}

这是完整的RecycleViewer:

public class GameListFragment extends Fragment {

private RecyclerView mGameRecycleViewer;
private GameAdapter mAdapter;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_game_list, container, false);
    mGameRecycleViewer = (RecyclerView) view
            .findViewById(R.id.game_recycler_view);
    mGameRecycleViewer.setLayoutManager(new LinearLayoutManager(getActivity()));

    updateUI();

    return view;
}

private class GameHolder extends RecyclerView.ViewHolder {
    public GameHolder(LayoutInflater inflater, ViewGroup parent) {
        super(inflater.inflate(R.layout.list_game_item, parent, false));
    }

}
private class GameAdapter extends RecyclerView.Adapter<GameHolder> {
    private List<Games> mGames;
    public GameAdapter(List<Games> crimes) {
        mGames = crimes;
    }
    @Override
    public GameHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
        return new GameHolder(layoutInflater, parent);
    }
    @Override
    public void onBindViewHolder(GameHolder holder, int position) {
    }
    @Override
    public int getItemCount() {
        return mGames.size();
    }

}
ArrayList<Games> games = new ArrayList<>();

private void updateUI() {
    //GameLab gamelab = GameLab.get(getActivity());
    //ArrayList<Games> crimes = gamelab.getGames();
    //for (int i = 0; i < 100; i++) {
        //Games g = new Games("t1",110);
        //g.setTeam2("T2");
        //g.setTeam2_score(120);
        //games.add(g);
    //}
    class run extends AsyncTask<Void,Void,Void> {
        String line = "";
        String data = "";
        @Override
        protected Void doInBackground(Void... voids) {

            try {

                URL url = new URL("https://stats.nba.com/stats/scoreboard/?GameDate=02/13/2019&LeagueID=00&DayOffset=0");

                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                InputStream is = conn.getInputStream();
                BufferedReader bf = new BufferedReader(new InputStreamReader(is));

                while(line!=null){
                    line = bf.readLine();
                    data += line;
                }
                JSONObject jo = new JSONObject(data);

                JSONArray ja = (JSONArray) jo.get("resultSets");
                JSONObject jo2 = (JSONObject) ja.get(1);//Linescores
                JSONArray ja3 = (JSONArray) jo2.get("rowSet");
                JSONArray ja4;

                int count = 0;
                for(int i = 0;i<ja3.length();i++){
                    ja4 = new JSONArray(ja3.get(i).toString());
                    //teams+=ja4.get(4)+": score =  "+ja4.get(21).toString()+"\n";
                    if(count == 1) {
                        games.get(games.size() - 1).setTeam2(ja4.get(4).toString());
                        games.get(games.size() - 1).setTeam2_score(Integer.parseInt(ja4.get(21).toString()));
                        count = 0;
                    }else{
                        games.add(new Games(ja4.get(4).toString(), Integer.parseInt(ja4.get(21).toString())));
                        count = 1;
                    }
                    Log.d("AAAAAHHHHHHH",Integer.toString(games.size()));
                }




            }catch (Exception e){
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            //main.setText(teams);

        }
    }
    run r = new run();

    r.execute();
    mAdapter = new GameAdapter(games);
    mGameRecycleViewer.setAdapter(mAdapter);
}

}

在获取json数据之前已经设置了适配器,因此可能正在发送一个空列表。 列表中已完全填充数据后,应在onPostExecute中设置适配器:

 @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        //main.setText(teams);
        mAdapter = new GameAdapter(games);
        mGameRecycleViewer.setAdapter(mAdapter);

    }
  相关解决方案