public class MainActivity extends Activity {
ImageView logo ;
JSONArray jArray;
String result = null;
InputStream is = null;
StringBuilder sb=null;
ListView top;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
logo = (ImageView) findViewById(R.drawable.logo_final_color);
top = (ListView) findViewById(R.id.list);
setContentView(R.layout.activity_main);
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("adresse^^");
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());
}
//convert response to string
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());
}
//paring data
int pr_id;
int pr_vote;
List<HashMap<String,String>> liste = new ArrayList<HashMap<String, String>>();
HashMap<String, String> element;
try{
jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
//Gives 1 back
pr_id=json_data.getInt("id_product");
//Gives 50 back
pr_vote=json_data.getInt("vote");
element = new HashMap<String, String>();
element.put("id", Integer.toString(pr_id));
element.put("vote", Integer.toString(pr_vote));
liste.add(element);
}
Log.i("test", liste.get(0).toString());
ListAdapter adapter = new SimpleAdapter(this.getBaseContext(),liste,R.layout.affichageitem,new String[] {"id", "vote"},
new int[] {R.id.id, R.id.vote });
if (adapter != null) {
top.setAdapter(adapter);
}
}
catch(JSONException e1){
Toast.makeText(getBaseContext(), "No product found" ,Toast.LENGTH_LONG).show();
} catch (ParseException e2) {
Log.e("log_tag", "Parse exception "+e2.toString());
}
}
}
Je ne m'en sors plus alors je demande de l'aide.
Merci.
[EDIT] PROBLEM SOLVED!
[Android] Listview and SimpleAdapter
× Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
× Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.