gpt4 book ai didi

android - 将已解析的 JSON 添加到 ListView 并显示它

转载 作者:行者123 更新时间:2023-11-29 15:00:27 26 4
gpt4 key购买 nike

public class GithubTab extends Fragment implements AdapterView.OnItemClickListener {

ListView repoListView;

private ListAdapter adapter;
private List<RepositoryItem> repoListItems;

private List<String> repoNameList;
private List<String> userNameList;
private List<String> descriptionList;

private TextView tvData;

private static final String TAG = "Github Tab";

Button buttonHit;
TextView resultText;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.github_tab, container, false);

repoListView = (ListView) view.findViewById(R.id.repoList);

repoListItems = new ArrayList<>();
repoNameList = new ArrayList<>();
userNameList = new ArrayList<>();
descriptionList = new ArrayList<>();

adapter = new ListAdapter(getContext(), repoListItems);
repoListView.setAdapter(adapter);
tvData = (TextView) view.findViewById(R.id.tvJsonItem);

// Clickable: able to open the GitHub webpage of the re

repoListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getContext(), "Clicked id" + view.getTag(), Toast.LENGTH_SHORT).show();
}
});

new JSONTask().execute("https://api.github.com/users/whyjay17/repos");

for(int i = 0; i < repoNameList.size(); i++) {
repoListItems.add(new RepositoryItem(i, repoNameList.get(i), userNameList.get(i), "ddd"));
}

return view;

}

public class JSONTask extends AsyncTask<String, String, String> {
@Override

// Any non-UI thread process is running in this method. After completion, it sends the result to OnPostExecute
protected String doInBackground(String... params) {

HttpURLConnection connection = null;
BufferedReader reader = null;

try {

.... Code Hidden ....

return retreivedJson;

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//cant close null

if (connection != null) {
// close both connection and the reader
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}

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

return null;

}


public void formatJSONArray(String results){
try {
JSONArray jsonArray = new JSONArray(results);
for(int i = 0; i < jsonArray.length(); i++){
JSONObject jsonObject=jsonArray.getJSONObject(i);
if(jsonObject.optString("name") != null) {
//tvData.append(jsonObject.getString("name"));
repoNameList.add(jsonObject.getString("name"));

//Toast.makeText(getContext(), "1 " + repoNameList.get(1), Toast.LENGTH_SHORT).show();
}

if(jsonObject.optJSONObject("owner") != null){
JSONObject ownerObject=jsonObject.getJSONObject("owner");

if(ownerObject.optString("login")!=null) {
//tvData.append(ownerObject.getString("login"));
userNameList.add(ownerObject.getString("login"));
//ownerObject.append(ownerObject.getString("avatar_url"));
}
}
}


}catch (JSONException jsonException){

}
}

/*
* Called after the background computation finishes. Result of doInBackground is passed in as a parameter.
*
* */
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);

/* for JSONArray data*/
if(result!=null && !result.isEmpty()) {
formatJSONArray(result);

}

}
}
}

上面的代码主要是尝试从 https://api.github.com/users/famous/repos 中解析一个 JSON 数据。 ,将一些特定信息(repo 名称、id、描述)添加到相应的列表,并尝试在我创建的 listView 上显示它。

当我对信息进行硬编码时,listView 可以工作(这意味着 listView 本身没有问题),但是当我尝试将数据放入列表中时(它具有已解析的 JSON 信息,我测试过它是实际上在列表中),它给了我一个空列表。

我怎样才能让它工作?

最佳答案

数据来自异步,因此在 onCreateView() 中,列表数据可能尚未准备好添加到适配器。

您需要将向 ListView 适配器添加元素的代码移动到 onPostExecute() 中,在 formatJSONArray() 方法之后,然后调用 notifyDatasetChange() 使 ListView 无效

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);

/* for JSONArray data*/
if(result!=null && !result.isEmpty()) {
formatJSONArray(result);
for(int i = 0; i < repoNameList.size(); i++) {
repoListItems.add(new RepositoryItem(i,
repoNameList.get(i), userNameList.get(i), "ddd"));
}

adapter.notifyDatasetChanged();
}
}

关于android - 将已解析的 JSON 添加到 ListView 并显示它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46884400/

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