gpt4 book ai didi

java - JSON 下载后 ListView 不更新

转载 作者:行者123 更新时间:2023-12-02 01:21:20 27 4
gpt4 key购买 nike

我正在尝试创建一个简单的 Android 应用程序,我从 JSON 获取数据并将其添加到 ListView 中的自定义行。

我确实从 JSON 中获取了数据,但没有显示任何行。我发现这是因为行是在 JSON 完成下载之前“创建”的。下载 JSON 后如何更新 ListView?

public class MainActivity extends AppCompatActivity {

private RequestQueue mQueue;
private static final String TAG = "MainActivity";
String isDoneLoading = "false";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Log.d(TAG, "LOG STARTED");
mQueue = Volley.newRequestQueue(this);

ListView listView = (ListView) findViewById(R.id.listView);
ArrayList<News> newsList = new ArrayList<>();

// Create news objects

jsonParse(newsList);

NewsListAdapter adapter = new NewsListAdapter(this, R.layout.row, newsList);

listView.setAdapter(adapter);

Log.d(TAG, "RELOADING LISTVIEW");

}


private void jsonParse(final ArrayList<News> newsArray) {

String url = "https://api.myjson.com/bins/kp9wz";

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("employees");

for (int i = 0; i < jsonArray.length(); i++) {
JSONObject news_feed = jsonArray.getJSONObject(i);

String headline = news_feed.getString("firstname");
String category = news_feed.getString("mail");

Log.i(TAG, headline);

News newsData = new News(headline, category);

newsArray.add(newsData);
}

} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});

mQueue.add(request);
}
}

最佳答案

尝试进行此更改

private RequestQueue mQueue;
private static final String TAG = "MainActivity";
String isDoneLoading = "false";
private NewsListAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Log.d(TAG, "LOG STARTED");
mQueue = Volley.newRequestQueue(this);

ListView listView = (ListView) findViewById(R.id.listView);
ArrayList<News> newsList = new ArrayList<>();


// Create news objects


jsonParse(newsList);



adapter = new NewsListAdapter(this, R.layout.row, newsList);

listView.setAdapter(adapter);

Log.d(TAG, "RELOADING LISTVIEW");


}


private void jsonParse(final ArrayList<News> newsArray) {

String url = "https://api.myjson.com/bins/kp9wz";

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("employees");


for (int i = 0; i < jsonArray.length(); i++) {
JSONObject news_feed = jsonArray.getJSONObject(i);

String headline = news_feed.getString("firstname");
String category = news_feed.getString("mail");

Log.i(TAG, headline);

News newsData = new News(headline, category);

newsArray.add(newsData);
}

updateList();

} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});

mQueue.add(request);

}

private void updateList(){
if(adapter != null){
adapter.notifyDataSetChanged();
}
}

关于java - JSON 下载后 ListView 不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57625416/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com