我正在从 URL 获取和解析 JSON 数据。
我想知道如何调整我的代码以在 ListView 而不是 TextView 中显示数据,因为我需要操作数据才能从列表中删除和更新记录。
我有以下代码:
public class fetchData extends AsyncTask<Void,Void,Void> {
String list = "";
String dataParsed = "";
String singleParsed = "";
@Override
protected Void doInBackground(Void... voids) {
try {
URL url = new URL("https://gonee.000webhostapp.com/Computadora.json");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while(line != null){
line = bufferedReader.readLine();
list = list + line;
}
JSONArray JA = new JSONArray(list);
for (int i = 0; i < JA.length(); i++) {
JSONObject JO = (JSONObject) JA.get(i);
singleParsed = "Id: " + JO.get("id") + "\n" +
"Marca: " + JO.get("Marca") + "\n" +
"Modelo: " + JO.get("Modelo") + "\n" +
"Precio: " + JO.get("Precio") + "\n\n";
dataParsed = dataParsed + singleParsed;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
MainActivity.list.setText(this.dataParsed);
}
}