Advertisement
moonlightcheese

Untitled

Jun 22nd, 2011
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.83 KB | None | 0 0
  1. case DIALOG_WIFI_PREF:
  2. {
  3.     final Command delete = new Command() {
  4.             public void execute() {
  5.                 removeDialog(DIALOG_WIFI_PREF);
  6.                 showDialog(DIALOG_WIFI_PREF);
  7.             }
  8.         };
  9.     Dialog dialog = new Dialog(this);
  10.     final View dialogLayout = inflater.inflate(R.layout.dialog_wifi_pref, null);
  11.     builder = new AlertDialog.Builder(this);
  12.     builder.setView(dialogLayout);
  13.     builder.setTitle("IP Configuration");
  14.     final EditText ipIn = (EditText)dialogLayout.findViewById(R.id.wifi_ip_in);
  15.     final EditText portIn = (EditText)dialogLayout.findViewById(R.id.wifi_port_in);
  16.     final EditText labelIn = (EditText)dialogLayout.findViewById(R.id.site_label_in);
  17.     final EditText codeIn = (EditText)dialogLayout.findViewById(R.id.activation_code);
  18.     final Spinner siteSpn = (Spinner)dialogLayout.findViewById(R.id.site_spn);
  19.     //final Button deleteBtn = (Button)dialogLayout.findViewById(R.id.delete_btn);
  20.     Cursor tempCur = null;
  21.     try {
  22.         tempCur = mDb.rawQuery("SELECT * FROM " + smsDbSchema.SiteSchema.TABLE_NAME, null);
  23.     } catch(Exception e) {
  24.         Log.e(LOG_TAG, "db query error: "+e.getMessage());
  25.     }
  26.     final Cursor cur = tempCur;
  27.    
  28.     final SimpleCursorAdapter tempAdapter = new SimpleCursorAdapter(
  29.         this,
  30.         android.R.layout.simple_spinner_item,
  31.         cur,
  32.         new String[] { smsDbSchema.SiteSchema.COLUMN_LABEL },
  33.         new int[] { android.R.id.text1 }
  34.     );
  35.     tempAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  36.     siteSpn.setAdapter(tempAdapter);
  37.    
  38.     //fill the initial values
  39.     String initSite = pref.getString("site_id", "New Site");
  40.     String spnLabel = null;
  41.     final Cursor initCur = mDb.query(smsDbSchema.SiteSchema.TABLE_NAME, null, smsDbSchema.SiteSchema.COLUMN_LABEL + "=?", new String[] { initSite }, null, null, null);
  42.     initCur.moveToFirst();
  43.     cur.moveToFirst();
  44.     if(initCur.getCount()>0) {
  45.         ipIn.setText(initCur.getString(initCur.getColumnIndex(smsDbSchema.SiteSchema.COLUMN_IP)));
  46.         portIn.setText(initCur.getString(initCur.getColumnIndex(smsDbSchema.SiteSchema.COLUMN_PORT)));
  47.         codeIn.setText(initCur.getString(initCur.getColumnIndex(smsDbSchema.SiteSchema.COLUMN_ACTIVATION_CODE)));
  48.         spnLabel = initCur.getString(initCur.getColumnIndex(smsDbSchema.SiteSchema.COLUMN_LABEL));
  49.         labelIn.setText(spnLabel, TextView.BufferType.SPANNABLE);
  50.         if(cur.getCount()>0) {
  51.             do {
  52.                 if(spnLabel.equals(cur.getString(cur.getColumnIndex(smsDbSchema.SiteSchema.COLUMN_LABEL)))) {
  53.                     siteSpn.setSelection(cur.getPosition());
  54.                 }
  55.             } while(cur.moveToNext());
  56.         }
  57.     }
  58.    
  59.     siteSpn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  60.             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  61.                 cur.moveToFirst();
  62.                 if(cur.getCount()>0) {
  63.                     do {
  64.                         String tempLabel = cur.getString(cur.getColumnIndex(smsDbSchema.SiteSchema.COLUMN_LABEL));
  65.                         if(tempLabel.equals(((TextView)view.findViewById(android.R.id.text1)).getText().toString())) {
  66.                             labelIn.setText(tempLabel, TextView.BufferType.SPANNABLE);
  67.                             labelIn.selectAll();
  68.                             portIn.setText(cur.getString(cur.getColumnIndex(smsDbSchema.SiteSchema.COLUMN_PORT)));
  69.                             ipIn.setText(cur.getString(cur.getColumnIndex(smsDbSchema.SiteSchema.COLUMN_IP)));
  70.                             codeIn.setText(cur.getString(cur.getColumnIndex(smsDbSchema.SiteSchema.COLUMN_ACTIVATION_CODE)));
  71.                         }
  72.                     } while(cur.moveToNext());
  73.                 }
  74.             }
  75.            
  76.             public void onNothingSelected(AdapterView<?> parent) {
  77.                 //
  78.             }
  79.         });
  80.    
  81.     labelIn.setOnFocusChangeListener(new View.OnFocusChangeListener() {
  82.             public void onFocusChange(View v, boolean hasFocus) {
  83.                 if(hasFocus) {
  84.                     EditText et = (EditText)v;
  85.                     String temp = et.getText().toString();
  86.                     et.setText(temp, TextView.BufferType.SPANNABLE);
  87.                     et.selectAll();
  88.                 }
  89.             }
  90.         });
  91.    
  92.     builder.setNeutralButton("Delete", new CommandWrapper(delete) {
  93.             public void onClick(DialogInterface dialog, int which) {
  94.                 //do things
  95.                 cur.moveToFirst();
  96.                 while(cur.moveToNext()) {
  97.                     String tempLabel = cur.getString(cur.getColumnIndex(smsDbSchema.SiteSchema.COLUMN_LABEL));
  98.                     View selectedSiteView = siteSpn.getSelectedView();
  99.                     String label = ((TextView)selectedSiteView.findViewById(android.R.id.text1)).getText().toString();
  100.                     if(tempLabel.equals(label)) {
  101.                         mDb.delete(smsDbSchema.SiteSchema.TABLE_NAME, smsDbSchema.SiteSchema.COLUMN_LABEL+"=?", new String[] { label });
  102.                     }
  103.                 }
  104.                 //TODO: complete this code to auto-select the next site after deletion
  105.                 //Cursor siteCur = mDb.rawQuery("SELECT * FROM " + smsDbSchema.SiteSchema.TABLE_NAME, null);
  106.                 //if(siteCur.getCount()>0) {
  107.                 //  //place "ip" and "port" of first site record in SharedPreferences
  108.                 //}
  109.                 //siteCur.close();
  110.                 dialog.dismiss();
  111.                 this.execute();
  112.             }
  113.         });
  114.    
  115.     builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
  116.             public void onClick(DialogInterface dialog, int id) {
  117.                 cur.moveToFirst();
  118.                 boolean newRecord = true;
  119.                 do {
  120.                     String tempLabel = null;
  121.                     if(cur.getCount()>0) {
  122.                         tempLabel = cur.getString(cur.getColumnIndex(smsDbSchema.SiteSchema.COLUMN_LABEL));
  123.                     }
  124.                     if(tempLabel!=null && tempLabel.equals(labelIn.getText().toString())) {
  125.                         //update
  126.                         ContentValues cv = new ContentValues();
  127.                         cv.put(smsDbSchema.SiteSchema.COLUMN_IP, ipIn.getText().toString());
  128.                         cv.put(smsDbSchema.SiteSchema.COLUMN_PORT, portIn.getText().toString());
  129.                         cv.put(smsDbSchema.SiteSchema.COLUMN_ACTIVATION_CODE, codeIn.getText().toString());
  130.                         smsMobile.this.mDb.update(smsDbSchema.SiteSchema.TABLE_NAME, cv, smsDbSchema.SiteSchema.COLUMN_LABEL+"=?", new String[] { tempLabel });
  131.                         newRecord = false;
  132.                         break;
  133.                     }
  134.                 } while(cur.moveToNext());
  135.                 if(newRecord) {
  136.                     //new entry
  137.                     ContentValues cv = new ContentValues();
  138.                     cv.put(smsDbSchema.SiteSchema.COLUMN_IP, ipIn.getText().toString());
  139.                     cv.put(smsDbSchema.SiteSchema.COLUMN_PORT, portIn.getText().toString());
  140.                     cv.put(smsDbSchema.SiteSchema.COLUMN_LABEL, labelIn.getText().toString());
  141.                     cv.put(smsDbSchema.SiteSchema.COLUMN_ACTIVATION_CODE, codeIn.getText().toString());
  142.                     smsMobile.this.mDb.insert(smsDbSchema.SiteSchema.TABLE_NAME, null, cv);
  143.                 }
  144.                 SharedPreferences.Editor editor = pref.edit();
  145.                 editor.putString("site_id", labelIn.getText().toString());
  146.                 editor.putString("activation", codeIn.getText().toString());
  147.                 editor.commit();
  148.                 //smsActivity.this.writeCSVFile("dashboard_settings.csv");
  149.                 dialog.dismiss();
  150.             }
  151.         });
  152.     builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  153.             public void onClick(DialogInterface dialog, int id) {
  154.                 dialog.cancel();
  155.             }
  156.         });
  157.     dialog = builder.create();
  158.     //InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
  159.     //mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);
  160.     //mgr.toggleSoftInput(0,0);
  161.     return dialog;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement