sto cominciando lo studio di android per creare un piccola applicazione che deve connettersi a un db mysql.
lasciando perdere l'ottimizzazione del codice (poi lo metterò a posto) vorrei effettuare una semplice query e visualizzare i risultati.
come componente ho scelto GridView che mi sembrava adatta.
seguendo un tutorial ho prima creato un file php che crea un oggetto JSON.
e poi ho messto questa roba qua:
ho già aggiunto la GV nel designer.codice:public class DBManagerActivity extends Activity { JSONArray jArray; String result = null; InputStream is = null; StringBuilder sb = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://host/android.php"); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); } catch (Exception e) { Log.e("log_tag", "Error in http connection" + e.toString()); } try { BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); sb = new StringBuilder(); sb.append(reader.readLine() + "\n"); String line = "0"; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); result = sb.toString(); } catch (Exception e) { Log.e("log_tag", "Error converting result " + e.toString()); } try { jArray = new JSONArray(result); JSONObject json_data = null; for (int i = 0; i < jArray.length(); i++) { json_data = jArray.getJSONObject(i); int id = json_data.getInt("id_movimento"); int entrata = json_data.getInt("entrata"); } } catch (JSONException e1) { Toast.makeText(getBaseContext(), "No City Found", Toast.LENGTH_LONG).show(); } catch (ParseException e1) { e1.printStackTrace(); } GridView gd = (GridView) findViewById(R.id.gridView1); } }
ora però mi sono bloccato.
come faccio a dirgli di visualizzare i dati nelle GV?

Rispondi quotando