[英]One activity 2 buttons which can go different activities
From an activity with some sensor data and two buttons, one to show graphs and another to display its parameters. 来自具有一些传感器数据和两个按钮的活动,一个用于显示图形,另一个用于显示其参数。
Question: 题:
When you click on one of the buttons it must go its activity. 当您单击其中一个按钮时,它必须进行活动。 It seems obvious, but take in mind that the object can be "barometer" or "TemperaturaHuemdadDht22".
这似乎很明显,但请记住,对象可以是“晴雨表”或“TemperaturaHuemdadDht22”。
From a list, when I click on one of its components, the component opens another screen and gives me all data from that component. 从列表中,当我单击其中一个组件时,该组件将打开另一个屏幕并向我提供该组件的所有数据。 These components are in an external database MySql and is synchronized with an internal SQlite then populate with data synchronized list and table.
这些组件位于外部数据库MySql中,并与内部SQlite同步,然后填充数据同步列表和表。 In the example shown there are two components, but may be 100 or 50 ...
在所示的示例中,有两个组件,但可能是100或50 ...
Once you clic to data sensor button then you go to another activity , or Barometro.java or TemperaturaHumedadDht22.java 一旦你clic到数据传感器按钮然后你去另一个活动,或Barometro.java或TemperaturaHumedadDht22.java
Pls check the Scheme: Scheme 请查看Scheme: Scheme
Specially in code check public void onClick (View v) method 特别是在代码中检查public void onClick(View v)方法
if (v == accesodata) {
Intent i = new Intent(ActividadInsercionObjeto.this, **barometro.class** or **TemperaturaHuemdadDht22.class** );
i.putExtra("IdentidadEnviada", (Serializable) campoIdObjeto.getText().toString());
startActivity(i);
Code : 代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.actividad_insercion_objeto);
// Encontrar Referencias UI
campodescripcionNombre = (TextView) findViewById(R.id.campo_descripcion_nombre);
campoMarca = (TextView) findViewById(R.id.campo_marca);
campoModelo = (TextView) findViewById(R.id.campo_modelo);
campoCorreo = (TextView) findViewById(R.id.campo_correo);
campoIdObjeto = (TextView) findViewById(R.id.campo_idObjeto);
accesodata = (Button) findViewById(R.id.accesodata);
accesodata.setOnClickListener(this);
accesotabla = (Button) findViewById(R.id.accesotabla);
accesotabla.setOnClickListener(this);
// Determinar si es detalle
String uri = getIntent().getStringExtra(URI_OBJETO);
if (uri != null) {
setTitle(R.string.titulo_actividad_editar_objeto);
uriObjeto = Uri.parse(uri);
getSupportLoaderManager().restartLoader(1, null, this);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_insercion_objeto, menu);
// Verificación de visibilidad acción eliminar
if (uriObjeto != null) {
menu.findItem(R.id.accion_eliminar).setVisible(true);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.accion_confirmar:
insertar();
break;
case R.id.accion_eliminar:
eliminar();
break;
}
return super.onOptionsItemSelected(item);
}
private void insertar() {
// Extraer datos de UI
String descripcionNombre = campodescripcionNombre.getText().toString();
String marca = campoMarca.getText().toString();
String modelo = campoModelo.getText().toString();
String correo = campoCorreo.getText().toString();
String IdentidadObjeto = campoIdObjeto.getText().toString();
// Validaciones y pruebas de cordura
if (!esNombreValido(descripcionNombre)) {
TextInputLayout mascaraCampoNombre = (TextInputLayout)findViewById(R.id.mascara_campo_nombre);
// esta linea la he añadido, si da fallo eliminar. Sujerida por corrector
assert mascaraCampoNombre != null;
// esta linea la he añadido, si da fallo eliminar. Sujerida por corrector fin
mascaraCampoNombre.setError("este campo no puede quedar vacio");
} else {
ContentValues valores = new ContentValues();
// Verificación: ¿Es necesario generar un id?
if (uriObjeto == null) {
valores.put(Objetos.ID_OBJETO, Objetos.generarIdObjeto());
}
valores.put(Objetos.DESCRIPCION_NOMBRE, descripcionNombre);
valores.put(Objetos.MARCA_MARCA, marca);
valores.put(Objetos.MODELO, modelo);
valores.put(Objetos.CORREO, correo);
valores.put(Objetos.VERSION, UTiempo.obtenerTiempo());
// Iniciar inserción|actualización
new TareaAnadirObjeto(getContentResolver(), valores).execute(uriObjeto);
finish();
}
}
private boolean esNombreValido(String nombre) {
return !TextUtils.isEmpty(nombre);
}
private void eliminar() {
if (uriObjeto != null) {
// Iniciar eliminación
new TareaEliminarObjeto(getContentResolver()).execute(uriObjeto);
finish();
}
}
private void poblarViews(Cursor data) {
if (!data.moveToNext()) {
return;
}
// Asignar valores a UI
campodescripcionNombre.setText(UConsultas.obtenerString(data, Objetos.DESCRIPCION_NOMBRE));
campoMarca.setText(UConsultas.obtenerString(data, Objetos.MARCA_MARCA));
campoModelo.setText(UConsultas.obtenerString(data, Objetos.MODELO));
campoCorreo.setText(UConsultas.obtenerString(data, Objetos.CORREO));
campoIdObjeto.setText(UConsultas.obtenerString(data, Objetos.ID_OBJETO));
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(this, uriObjeto, null, null, null, null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
poblarViews(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
}
// @Override
public void onClick (View v) {
if (v == accesotabla) {
Intent i = new Intent(ActividadInsercionObjeto.this, GraficaHumedadTemperatura.class);
i.putExtra("IdentidadEnviada", (Serializable) campoIdObjeto.getText().toString());
startActivity(i);
}
if (v == accesodata) {
Intent i = new Intent(ActividadInsercionObjeto.this, **DEPENEDSONWHICHBUTTONYOUCLICK.class**);
i.putExtra("IdentidadEnviada", (Serializable) campoIdObjeto.getText().toString());
startActivity(i);
}
}
static class TareaAnadirObjeto extends AsyncTask<Uri, Void, Void> {
private final ContentResolver resolver;
private final ContentValues valores;
public TareaAnadirObjeto(ContentResolver resolver, ContentValues valores) {
this.resolver = resolver;
this.valores = valores;
}
@Override
protected Void doInBackground(Uri... args) {
Uri uri = args[0];
if (null != uri) {
/*
Verificación: Si el cobjeto que se va a actualizar aún no ha sido sincronizado,
es decir su columna 'insertado' = 1, entonces la columna 'modificado' no debe ser
alterada
*/
Cursor c = resolver.query(uri, new String[]{Objetos.INSERTADO}, null, null, null);
if (c != null && c.moveToNext()) {
// Verificación de sincronización
if (UConsultas.obtenerInt(c, Objetos.INSERTADO) == 0) {
valores.put(Objetos.MODIFICADO, 1);
}
valores.put(Objetos.VERSION, UTiempo.obtenerTiempo());
resolver.update(uri, valores, null, null);
}
} else {
resolver.insert(Objetos.URI_CONTENIDO, valores);
}
return null;
}
}
static class TareaEliminarObjeto extends AsyncTask<Uri, Void, Void> {
private final ContentResolver resolver;
public TareaEliminarObjeto(ContentResolver resolver) {
this.resolver = resolver;
}
@Override
protected Void doInBackground(Uri... args) {
/*
Verificación: Si el registro no ha sido sincronizado aún, entonces puede eliminarse
directamente. De lo contrario se marca como 'eliminado' = 1
*/
Cursor c = resolver.query(args[0], new String[]{Objetos.INSERTADO}
, null, null, null);
int insertado;
if (c != null && c.moveToNext()) {
insertado = UConsultas.obtenerInt(c, Objetos.INSERTADO);
} else {
return null;
}
if (insertado == 1) {
resolver.delete(args[0], null, null);
} else if (insertado == 0) {
ContentValues valores = new ContentValues();
valores.put(Objetos.ELIMINADO, 1);
resolver.update(args[0], valores, null, null);
}
return null;
}
}
} }
I don't know how you want to handle this 我不知道你想怎么处理这件事
it is determinated by the name of the sensor showed in "ActividadInsertarObjeto.java"
它由“ActividadInsertarObjeto.java”中显示的传感器名称决定
But you seem to understand you need to check that value, so just do it 但你似乎明白你需要检查那个值,所以就这样做吧
if (v == accesodata) {
Class c = null;
if (someCondition) { // TODO: figure out what you should check
c = Barometro.class;
} else {
c = TemperaturaHuemdadDht22.class;
}
Intent i = new Intent(ActividadInsercionObjeto.this, c);
i.putExtra("IdentidadEnviada", (Serializable) campoIdObjeto.getText().toString());
startActivity(i);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.