[英]How to display fetched json data into listView using ArrayAdapter
[英]How to display json data in a listview?
我想在列表視圖中查看json數據...
我試圖創建一個新的對象類並收集要列出的數據,但我認為我在某些方面是錯誤的...
我試圖使用TextView來驗證數據的正確傳遞,並且似乎可以正常工作,但是我需要將它們顯示在listView上。您有解決我問題的想法嗎?
public class LoggedActivity extends AppCompatActivity {
private RequestQueue mQueue;
private ListView lstView;
private TextView mTextViewResult;
Button buttonParse;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_logged);
lstView = findViewById(R.id.lstView);
mTextViewResult = findViewById(R.id.text_view_result);
buttonParse = findViewById(R.id.button_parse);
Bundle extras = getIntent().getExtras();
final String id_utente = extras.getString("id");
mQueue = Volley.newRequestQueue(this);
buttonParse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
jsonParse(id_utente);
}
});
}
private void jsonParse(String id_utente) {
class Prodotti {
private String id;
private String idUtente;
private String nomeProdotto;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getIdUtente() {
return idUtente;
}
public void setIdUtente(String idUtente) {
this.idUtente = idUtente;
}
public String getNomeProdotto() {
return nomeProdotto;
}
public void setNomeProdotto(String nomeProdotto) {
this.nomeProdotto = nomeProdotto;
}
}
final List<Prodotti> listaProdotti = new ArrayList<Prodotti>();
String url = "http://192.168.1.5/progettoPHP/WebServices/webSrv.php?type=recuperaProdotti&idutente=" + id_utente;
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("Dati");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject dato = jsonArray.getJSONObject(i);
Prodotti prdt = new Prodotti();
prdt.setId(dato.getString("id"));
prdt.setIdUtente(dato.getString("id_utente"));
prdt.setNomeProdotto(dato.getString("nome_prodotto"));
listaProdotti.add(prdt);
//mTextViewResult.append(id + ", " + id_utente + ", " + nome_prodotto + "\n\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
mQueue.add(request);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.