1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView list = (ListView)findViewById(R.id.listalarm);
AlarmeAdapter adapter = new AlarmeAdapter(this, res);
adapter.addListener(this);
list.setAdapter(adapter);
new AsyncTask<Void,Void,List<Map<String,Object>>>()
{
@Override
protected List<Map<String, Object>> doInBackground(Void... voids)
{
List<Map<String, Object>> res = new ArrayList<Map<String, Object>>();
InputStream is = null;
String result = "";
ArrayList<NameValuePair> listAlr = new ArrayList<NameValuePair>();
listAlr.add(new BasicNameValuePair("alarm",""));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(strURL);
httppost.setEntity(new UrlEncodedFormEntity(listAlr));
HttpResponse response = httpclient.execute(httppost);
is = response.getEntity().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);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result=sb.toString();
Log.i("result", result);
}catch(Exception e)
{
Log.e("log_tag", "Error converting result " + e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++)
{
JSONObject json_data = jArray.getJSONObject(i);
Map<String, Object> map = new HashMap<String, Object>();
map.put("ID_ALARM", json_data.getInt("ID_ALARM"));
map.put("ID_OBJECT", json_data.getInt("ID_OBJECT"));
map.put("ID_GROUP",json_data.getInt("ID_GROUP"));
map.put("ID_PLACE", json_data.getInt("ID_PLACE"));
map.put("TIME_ALARM",json_data.getString("TIME_ALARM"));
map.put("PRIORITY_ALARM",json_data.getInt("PRORITY_ALARM"));
priority[i] = json_data.getInt("PRORITY_ALARM");
res.add(map);
Log.i("log_tag","ID_ALARM: "+json_data.getInt("ID_ALARM")+ ", ID_OBJECT: "+json_data.getInt("ID_OBJECT")+ ", priority: "+json_data.getString("PRORITY_ALARM"));
}
}catch(JSONException e)
{
Log.e("log_tag", "Error parsing data " + e.toString());
}
return res;
}
protected void onPostExecute(List<Map<String,Object>> res)
{
ListView list = (ListView)findViewById(R.id.listalarm);
list.setAdapter(new SimpleAdapter(BDDAlarm.this, res,R.layout.listitem, new String[]{"ID_ALARM","ID_OBJECT","ID_GROUP","ID_PLACE","TIME_ALARM" },new int[] { R.id.idAlarmTextView,R.id.idObjectTextView,R.id.idGroupTextView,R.id.idPlaceTextView,R.id.idTimeAlarm}));
list.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View v,int position, long arg3) {
lancementDetailAlarm(position);
System.out.println("Item click");
}
});
}
}.execute();
}
/**----------------------Method----------------------------*/
private void lancementDetailAlarm(int position) {
Intent intenalr1 = new Intent();
intenalr1.setClass(this, DetailsAlarm.class);
startActivity(intenalr1);
}
} |
Partager