I want to show a snackbar with data obtained through a database, the problem is that the snackbar is not shown, I will leave you 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;
}
});
but it already loads the snackbar and the data, only the message is the problem
Look at this code
If the connection is null, it does not create, therefore it seems that the error is that, that if it is null it does nothing, so it is not drawn, you must start the variable
con
and make sure that there is a connection to the database IF AND ONLY YES, you want you tosnackbarText
have data from the database, it should be something like this:YES, the scenario is different and the data is NOT NECESSARILY going to leave the database, you should follow the order you have
With the difference that
if (con == null)
apart from calling the methodisConnected(getApplicationContext());
draw yoursnackbarText
without the data from the base.You should check your connection since it is the one that determines that the SnackBar is displayed
It is also important, check inside your
LogCat
, since you could have a good connection, and correctly obtain theResultset
but when obtaining data like"tipo"
and"descripcion"
these are not actually obtained:The problem is you close the connection but the instance is null!
You must validate
or only close the connection at the end of the else, in which it is determined that the connection was made:
The error that you now mention:
It's because you're trying to get data from the cursor(
rs
) but it doesn't exist.