简体   繁体   English

一个活动2个按钮,可以进行不同的活动

[英]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.

相关问题 将少量活动绑定到一个服务-将类视为服务连接的类-不能破坏活动 - Binding few activities to one service - class which connected treated like service - can't destroy activity 如何将多个按钮从一个活动添加到Android中的不同活动 - How to add multple buttons from one activity to an a different to activity in android 我如何才能从一项活动中清除3项活动 - how can i clear the 3 activities from one activity 三个按钮,每个按钮以不同的Int启动相同的Activity。 但是,当选择任何按钮时,它将加载所有3个活动 - Three buttons, each start the same Activity with a different Int. But when selecting any button, it loads all 3 Activities 单击来自两个不同活动的两个单选按钮时,如何在新活动中显示文本? - How to display text in a new activity when two radio buttons from two different activities are clicked? 如何使用同一屏幕上的两个不同的按钮启动两个不同的活动? - how to launch two different activities with two different buttons which are in the same screen? StartActivityForResult 用于多个按钮访问一个活动并在选择时获取不同的数据? - StartActivityForResult for multiple buttons accessing one activity and getting different datas on selection? 如何使用按钮将String值从一个活动传递到两个不同的活动? - How do you pass String value using button from one activity to two different activities? 如何在 Android 中为不同的活动使用一个登录屏幕活动? - How do I use One login screen activity for different activities in Android? 两个按钮到两个不同活动的意图 - intent for two buttons to two different activities
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM