Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- // Permission StrictMode
- if (android.os.Build.VERSION.SDK_INT > 9) {
- StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
- StrictMode.setThreadPolicy(policy);
- }
- // spinner1
- final Spinner spin = (Spinner) findViewById(R.id.spinner1);
- String url = "http://localhost/customer/getCustomers.php";
- try
- {
- JSONArray data = new JSONArray(getJSONUrl(url));
- final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
- HashMap<String, String> map;
- for (int i = 0; i < data.length(); i++) {
- JSONObject c = data.getJSONObject(i);
- map = new HashMap<String, String>();
- map.put("customerID", c.getString("customerID"));
- map.put("name", c.getString("name"));
- map.put("phone", c.getString("phone"));
- MyArrList.add(map);
- }
- SimpleAdapter sAdap;
- sAdap = new SimpleAdapter(MainActivity.this, MyArrList, R.layout.activity_show,
- new String[]{"customerID", "name", "phone"}, new int[]{R.id.ColCustomerID, R.id.ColName, R.id.ColTel});
- spin.setAdapter(sAdap);
- final AlertDialog.Builder viewDetail = new AlertDialog.Builder(this);
- spin.setOnItemSelectedListener(new OnItemSelectedListener() {
- public void onItemSelected(AdapterView<?> arg0, View selectedItemView, int position, long id) {
- String sCustomerID = MyArrList.get(position).get("customerID").toString();
- String sName = MyArrList.get(position).get("name").toString();
- String sTel = MyArrList.get(position).get("phone").toString();
- viewDetail.setIcon(android.R.drawable.btn_star_big_on);
- viewDetail.setTitle("Customer Detail");
- viewDetail.setMessage("customerID : " + sCustomerID + "n" + "name : " + sName + "n" + "Tel : " + sTel);
- viewDetail.setPositiveButton("OK", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which)
- {
- dialog.dismiss();
- }
- });
- viewDetail.show();
- }
- public void onNothingSelected(AdapterView<?> arg0) {
- Toast.makeText(MainActivity.this, "Your Selected : Nothing", Toast.LENGTH_SHORT).show();
- }
- });
- }
- catch (JSONException e)
- {
- e.printStackTrace();
- }
- }
- public String getJSONUrl(String url) {
- StringBuilder str = new StringBuilder();
- HttpClient client = new DefaultHttpClient();
- HttpGet httpGet = new HttpGet(url);
- try {
- HttpResponse response = client.execute(httpGet);
- StatusLine statusLine = response.getStatusLine();
- int statusCode = statusLine.getStatusCode();
- if (statusCode == 200) { // Download OK
- HttpEntity entity = response.getEntity();
- InputStream content = entity.getContent();
- BufferedReader reader = new BufferedReader(new InputStreamReader(content));
- String line;
- while ((line = reader.readLine()) != null) {
- str.append(line);
- }
- } else {
- Log.e("Log", "Failed to download result..");
- }
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return str.toString();
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // getMenuInflater().inflate(R.menu.activity_main, menu);
- return true;
- }
- [{"customerID":"1","name":"john","phone":"0786333210"},
- {"customerID":"2","name":"sam","phone":"0786223210"},
- {"customerID":"3","name":"ali","phone":"0786543210"}]
- public String getJSONUrl(String url) {
- StringBuilder str = new StringBuilder();
- HttpClient client = new DefaultHttpClient();
- HttpGet httpGet = new HttpGet(url);
- try {
- HttpResponse response = client.execute(httpGet);
- StatusLine statusLine = response.getStatusLine();
- int statusCode = statusLine.getStatusCode();
- if (statusCode == 200) { // Download OK
- HttpEntity entity = response.getEntity();
- InputStream content = entity.getContent();
- BufferedReader reader = new BufferedReader (newInputStreamReader(content));
- String line;
- while ((line = reader.readLine()) != null) {
- str.append(line);
- }
- } else {
- Log.e("Log", "Failed to download result..");
- }
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return str.toString();
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // getMenuInflater().inflate(R.menu.activity_main, menu);
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement