-1

我尝试关于常量教程的 Commonsware教程

我修改活动

public class ConstantsBrowser extends ListActivity {
  private LocationManager lm;
  private LocationListener locListener;
  private TextView latTxt, lonTxt;

  Intent intent = null;
  private static final int ADD_ID = Menu.FIRST+1;
  private static final int DELETE_ID = Menu.FIRST+3;
  private static final int UPDATE_ID = Menu.FIRST+4;
  private static final int DETAIL_ID = Menu.FIRST+5;
  public static final int SHOW_SUB_ACTIVITY_VIEW=3;
  private DatabaseHelper db=null;
  private Cursor constantsCursor=null;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.banner);


    db=new DatabaseHelper(this);
    constantsCursor=db
                      .getReadableDatabase()
                      .rawQuery("SELECT _ID, alamat, tglb "+
                                "FROM constants ORDER BY _ID",
                                null);

    ListAdapter adapter=new SimpleCursorAdapter(this,
                          R.layout.row, constantsCursor,
                          new String[] {
                                        DatabaseHelper.ID,
                                        DatabaseHelper.ALAMAT,
                                        DatabaseHelper.TANGGAL_AKHIR},
                          new int[] {R.id.id, R.id.alamat, R.id.tglakhir});

    setListAdapter(adapter);
    registerForContextMenu(getListView());
  }

  @Override
  public void onDestroy() {
    super.onDestroy();

    constantsCursor.close();
    db.close();
  }



  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(Menu.NONE, ADD_ID, Menu.NONE, "Data Baru")
        .setIcon(R.drawable.add)
        .setAlphabeticShortcut('a');

    return(super.onCreateOptionsMenu(menu));
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case ADD_ID:
        add();
        return(true);
    }

    return(super.onOptionsItemSelected(item));
  }

  @Override
  public void onCreateContextMenu(ContextMenu menu, View v,
                                    ContextMenu.ContextMenuInfo menuInfo) {
    menu.add(Menu.NONE, DETAIL_ID, Menu.NONE, "Detail");
    menu.add(Menu.NONE, DELETE_ID, Menu.NONE, "Delete");
    menu.add(Menu.NONE, UPDATE_ID, Menu.NONE, "Update")
        .setAlphabeticShortcut('d');
  }

  @Override
  public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {

    case DETAIL_ID:
        AdapterView.AdapterContextMenuInfo info=
          (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();

        detail(info.id);
        return(true);

    case DELETE_ID:
        AdapterView.AdapterContextMenuInfo infoDetail=
          (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();

        delete(infoDetail.id);
        return(true);

      case UPDATE_ID:
          intent = new Intent(ConstantsBrowser.this, MainActivity.class);
            startActivityForResult(intent, SHOW_SUB_ACTIVITY_VIEW);
          return(true);
    }

    return(super.onOptionsItemSelected(item));
  }


  private LocationManager lman;
  private LocationListener locaListener;
  private TextView latTxtt, lonTxtt;


    private void add() {
    LayoutInflater inflater=LayoutInflater.from(this);
    View addView=inflater.inflate(R.layout.add_edit, null);
    latTxtt = (TextView) addView.findViewById(R.id.latitudeTxtt);
    lonTxtt = (TextView) addView.findViewById(R.id.longitudeTxtt);

    lman = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locaListener = new MyLocationListenerDialog();
    lman.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 10,locaListener);
    final DialogWrapper wrapper=new DialogWrapper(addView);

    new AlertDialog.Builder(this)
      .setTitle(R.string.add_title)
      .setView(addView)
      .setPositiveButton(R.string.ok,
                          new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,
                              int whichButton) {
          processAdd(wrapper);
        }
      })
      .setNegativeButton(R.string.cancel,
                          new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,
                              int whichButton) {
          // ignore, just dismiss
        }
      })
      .show();
  }

  private class MyLocationListenerDialog implements LocationListener {
        @Override
        public void onLocationChanged(Location loc) {
            // TODO Auto-generated method stub
            if (loc != null) {
                latTxtt.setText(String.valueOf(loc.getLatitude()));
                lonTxtt.setText(String.valueOf(loc.getLongitude()));
            }
        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

    }

  private void loadUser() {
        EditText id = (EditText) findViewById(R.id.idDetail);
        //View editTextHidden = (EditText) findViewById(R.id.txtHidden);
        // database handler
        DatabaseHelper db = new DatabaseHelper(getApplicationContext()); 
        // Spinner Drop down elements
        SQLiteDatabase dbs = db.getReadableDatabase();

        Cursor cursor = dbs.rawQuery("SELECT * FROM constants", null);
      if (cursor.moveToFirst()) {
         do {
            String username=cursor.getString(0); // Here you can get data from table and stored in string if it has only one string.

            id.setText(username);


         } while (cursor.moveToNext());
      }
      if (cursor != null && !cursor.isClosed()) {
         cursor.close();
      }
      if(db!=null)
      {
          db.close();
      }
        // Creating adapter for spinner


    }

  private void detail(final long rowId) {
        if (rowId>0) {

            LayoutInflater inflater=LayoutInflater.from(this);
            View addView=inflater.inflate(R.layout.detail_tetap, null);
            loadUser();
            final DialogWrapper wrapper=new DialogWrapper(addView);

          new AlertDialog.Builder(this)
            .setTitle(R.string.detail_title)

            .setView(addView)
            .setNegativeButton(R.string.cancel,
                                new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog,
                                    int whichButton) {
              // ignore, just dismiss
              }
            })
            .show();
        }
      }

  private void delete(final long rowId) {
    if (rowId>0) {
      new AlertDialog.Builder(this)
        .setTitle(R.string.delete_title)
        .setPositiveButton(R.string.ok,
                            new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog,
                                int whichButton) {
            processDelete(rowId);
          }
        })
        .setNegativeButton(R.string.cancel,
                            new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog,
                                int whichButton) {
          // ignore, just dismiss
          }
        })
        .show();
    }
  }

  private void processAdd(DialogWrapper wrapper) {
    ContentValues values=new ContentValues(2);

    values.put(DatabaseHelper.CODE, wrapper.getCode());
    values.put(DatabaseHelper.ALAMAT, wrapper.getAlamat());
    values.put(DatabaseHelper.BATAS, wrapper.getBatas());
    values.put(DatabaseHelper.LAT, wrapper.getLat());
    values.put(DatabaseHelper.LON, wrapper.getLon());
    values.put(DatabaseHelper.LUAS, wrapper.getLuas());
    values.put(DatabaseHelper.TANGGAL_AWAL, wrapper.getTglA());
    values.put(DatabaseHelper.TANGGAL_AKHIR, wrapper.getTglB());

    db.getWritableDatabase().insert("constants", DatabaseHelper.ALAMAT, values);
    constantsCursor.requery();
  }

  private void processDelete(long rowId) {
    String[] args={String.valueOf(rowId)};

    db.getWritableDatabase().delete("constants", "_ID=?", args);
    constantsCursor.requery();
  }

  class DialogWrapper {
    EditText codeField=null;
    EditText alamatField=null;
    EditText batasField=null;
    EditText luasField=null;
    EditText tglAField=null;
    EditText tglBField=null;
    TextView latField=null;
    TextView lonField=null;
    View base=null;

    DialogWrapper(View base) {
      this.base=base;
      tglBField=(EditText)base.findViewById(R.id.tglakhir);
    }

    String getCode() {
        return(getCodeField().getText().toString());
      }

    String getAlamat() {
      return(getAlamatField().getText().toString());
    }

    String getBatas() {
        return(getBatasField().getText().toString());
      }

    String getLuas() {
        return(getLuasField().getText().toString());
      }

    String getTglA() {
        return(getTglAField().getText().toString());
      }

    String getTglB() {
      return(getTglBField().getText().toString());
    }

    String getLat() {
        return(getLatField().getText().toString());
      }

    String getLon() {
        return(getLonField().getText().toString());
      }

    private EditText getCodeField() {
        if (codeField==null) {
          codeField=(EditText)base.findViewById(R.id.code);
        }
        return(codeField);
      }

    private EditText getAlamatField() {
      if (alamatField==null) {
          alamatField=(EditText)base.findViewById(R.id.alamat);
      }

      return(alamatField);
    }

    private EditText getBatasField() {
        if (batasField==null) {
          batasField=(EditText)base.findViewById(R.id.batas);
        }

        return(batasField);
      }

    private TextView getLatField() {
        if (latField==null) {
          latField=(TextView)base.findViewById(R.id.latitudeTxtt);
        }

        return(latField);
      }

    private TextView getLonField() {
        if (lonField==null) {
         lonField=(TextView)base.findViewById(R.id.longitudeTxtt);
        }

        return(lonField);
      }

    private EditText getLuasField() {
        if (luasField==null) {
          luasField=(EditText)base.findViewById(R.id.luas);
        }

        return(luasField);
      }

    private EditText getTglAField() {
        if (tglAField==null) {
            tglAField=(EditText)base.findViewById(R.id.tglawal);
        }

        return(tglAField);
      }

    private EditText getTglBField() {
      if (tglBField==null) {
          tglBField=(EditText)base.findViewById(R.id.tglakhir);
      }

      return(tglBField);
    }
  }
}

当我尝试显示细节时,我使用此代码

private void loadUser() {
        EditText id = (EditText) findViewById(R.id.idDetail);
        //View editTextHidden = (EditText) findViewById(R.id.txtHidden);
        // database handler
        DatabaseHelper db = new DatabaseHelper(getApplicationContext()); 
        // Spinner Drop down elements
        SQLiteDatabase dbs = db.getReadableDatabase();

        Cursor cursor = dbs.rawQuery("SELECT * FROM constants", null);
      if (cursor.moveToFirst()) {
         do {
            String username=cursor.getString(0); // Here you can get data from table and stored in string if it has only one string.

            id.setText(username);


         } while (cursor.moveToNext());
      }
      if (cursor != null && !cursor.isClosed()) {
         cursor.close();
      }
      if(db!=null)
      {
          db.close();
      }
        // Creating adapter for spinner


    }

  private void detail(final long rowId) {
        if (rowId>0) {

            LayoutInflater inflater=LayoutInflater.from(this);
            View addView=inflater.inflate(R.layout.detail_tetap, null);
            loadUser();
            final DialogWrapper wrapper=new DialogWrapper(addView);

          new AlertDialog.Builder(this)
            .setTitle(R.string.detail_title)

            .setView(addView)
            .setNegativeButton(R.string.cancel,
                                new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog,
                                    int whichButton) {
              // ignore, just dismiss
              }
            })
            .show();
        }
      }

我用一个编辑文本进行测试,但是当我启动应用程序时强制关闭。排队id.setText(username);

如何在sqlite中显示一些数据到alertDialog?

所以当我选择菜单detail()时对话框显示数据?

BR

亚历克斯

4

1 回答 1

2

如果idDetailEditText 在AlertDialog布局内,那么您将需要使用addView它来访问它:

EditText id = (EditText)addView. findViewById(R.id.idDetail);
于 2013-02-21T06:50:22.267 回答