snackbar with database,

1

I want to show a snackbar with data obtained through a database, the problem is that the snackbar is not shown, I will leave the code that I am using:

edit: delete the code and this is the current one

map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                @Override
                public boolean onMarkerClick(final Marker marker) {

                    View v1 = findViewById(R.id.content_main);
                    int height = 20;
                    int width = 20;
                    String query = "select top 1 tipo,descripcion from reporte where nombre_ruta = '"+marker.getTitle()+"';";
                    String query2="select AVG(valoracion)as valoracion from valorar where nombre_ruta = '"+marker.getTitle()+"';";


                    BitmapDrawable bitmapdraw = (BitmapDrawable) getResources().getDrawable(R.drawable.starmarcadores);
                    Bitmap b = bitmapdraw.getBitmap();
                    Bitmap smallMarker = Bitmap.createScaledBitmap(b, width, height, false);
                    Drawable drawable= new BitmapDrawable(getResources(), smallMarker);

                    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                    ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);

                    try {
                                    con = connectionclass (un, passwords, db, ip);
                                    if (con == null) {
                                        isConnected(getApplicationContext());
                                    } else if(con!=null){
                                        Log.d("este camino","este camino");
                                        stmt = con.prepareStatement(query);
                                        stmt.setQueryTimeout(2);
                                        rs = stmt.executeQuery();
                                        while (rs.next()) {
                                            Log.d("tipo",rs.getString("tipo"));
                                            Log.d("descripcion",rs.getString("descripcion"));

                                            SpannableStringBuilder snackbarText = new SpannableStringBuilder();
                                            snackbarText.append("Valoración: ");
                                            snackbarText.append("");
                                           // snackbarText.setSpan(span, 13, 14, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                                            snackbarText.append(System.getProperty("line.separator"));
                                            snackbarText.append("Estado de la ruta: posible " + rs.getString("tipo") );
                                            snackbarText.append(System.getProperty("line.separator"));
                                            snackbarText.append("Descripción del reporte: " + rs.getString("descripcion"));

                                            snackbar = Snackbar.make(v1, snackbarText, Snackbar.LENGTH_INDEFINITE)
                                                    .setActionTextColor(Color.YELLOW)
                                                    .setAction("Opciones", new View.OnClickListener() {
                                                        @Override
                                                        public void onClick(View v) {
                                                            new AlertDialog.Builder(MainActivity.this)
                                                                    .setIcon(R.drawable.cerrar)
                                                                    .setTitle("Opciones")
                                                                    .setMessage("Te gustaria valorar o reportar la ruta?")
                                                                    .setCancelable(true)
                                                                    .setPositiveButton("Valorar", new DialogInterface.OnClickListener() {
                                                                        @Override
                                                                        public void onClick(DialogInterface dialog, int which) {
                                                                            Intent i = new Intent(getApplicationContext(), Valoracion2.class);
                                                                            i.putExtra("Nruta", marker.getTitle().toString());
                                                                            startActivity(i);
                                                                        }
                                                                    })
                                                                    .setNeutralButton("Cancelar", null)
                                                                    .setNegativeButton("Reportar", new DialogInterface.OnClickListener() {
                                                                        @Override
                                                                        public void onClick(DialogInterface dialog, int which) {

                                                                        }
                                                                    }).show();
                                                        }
                                                    });
                                            View snackbarView = snackbar.getView();
                                            TextView textView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
                                            textView.setMaxLines(5);
                                            snackbar.show();
                                            con.close();
                                        }
                                    }
                    } catch (SQLException e) {
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
                    }
                    return false;
                }
            });

the problem now is this

but it already loads the snackbar and the data, only the message is the problem

    
asked by zhet 13.12.2016 в 04:08
source

2 answers

1

You must check your connection since it is the one that determines that the SnackBar is displayed

 con = connectionclass (un, passwords, db, ip);
    if (con == null) {
             isConnected(getApplicationContext());
     } else {
             //Muestra SnackBar.

It is also important to review within your LogCat , since you could have a good connection, and obtain correctly the Resultset but when you obtain data like "tipo" and "descripcion" these are not really obtained:

rs = stmt.executeQuery();
 if (rs.next()) {
    SpannableStringBuilder snackbarText = new SpannableStringBuilder();
    snackbarText.append("Valoración: ");
    snackbarText.append(valoracion + " ");
    snackbarText.setSpan(span, 13, 14, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    snackbarText.append(System.getProperty("line.separator"));
    snackbarText.append("Estado de la ruta: posible " +  rs.getString("tipo"));
    snackbarText.append(System.getProperty("line.separator"));
    snackbarText.append("Descripción del reporte: " + rs.getString("descripcion"));

The problem is closing the connection but the instance is null!

con.close();

You must validate

if(con != null){
  con.close();
}

or just close the connection at the end of the else, in which it is determined that the connection was made:

con = connectionclass (un, passwords, db, ip);
    if (con == null) {
        isConnected(getApplicationContext());
        Log.d("conexion nula","conexion nula");
    } else {
       ...
       ...
       con.close();
     }

The error that you are now commenting:

  

Invalid State: the resultSet object is closed ()

This is because you are trying to get cursor data ( rs ) but it does not exist.

    
answered by 13.12.2016 / 18:43
source
1

Look at this code

con = connectionclass(un, passwords, db, ip);
if (con == null) {
    isConnected(getApplicationContext());
    //creas el snackbarText sin los datos de la base
}else{ 
    //creas el snackbarText con los datos de la base
}

If the connection is null, it does not create therefore it seems that the error is that, that if it is null it does not do anything for that reason it is not drawn, you must start the variable con and make sure that there is a connection to the database YES AND ONLY YES, you want your snackbarText to have data from the database, it should be something like this:

con = connectionclass(un, passwords, db, ip);
if (con == null) {
    //aqui conectar correctamente la base de datos
}
//seguir el tratamiento
stmt = con.prepareStatement(query);
stmt.setQueryTimeout(1);
rs = stmt.executeQuery();

YES, the scenario is different and NOT NECESSARILY the data will come out of the database, you should follow the order you have

con = connectionclass(un, passwords, db, ip);
if (con == null) {
    isConnected(getApplicationContext());
}else{ 
    //creas el snackbarText 
}

With the difference that in if (con == null) apart from calling the method isConnected(getApplicationContext()); draw your snackbarText without the data of the base.

    
answered by 13.12.2016 в 15:17