gpt4 book ai didi

Java Arraylist 已满但不更新?

转载 作者:行者123 更新时间:2023-12-02 10:11:25 28 4
gpt4 key购买 nike

我正在尝试制作一个可以获取实时游戏得分的应用程序,但在使用 recycleviewer 和 arraylist 时遇到了问题。

RecycleView 工作正常,但当我制作数组列表来填充 RecycleView 时,我遇到了麻烦。这里我只是用 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);

}

关于Java Arraylist 已满但不更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54976808/

28 4 0