How to refer from a ready adapter to the context that inflates it

0
  

Good I have a problem and it is that when inflating a list I am created an event on click of an item, this works without problems: it opens me without problems the following window, but here comes the problem, using the same context no let me close it, that is, it does not let me make a context.finish();   How can I close it?   Thanks

public class adaptador_lista_paises extends ArrayAdapter<String> {


    private Context context;
    private ArrayList<String> nombrePaises = new ArrayList<>();

    public adaptador_lista_paises(Context context, ArrayList<String> nombrePaises) {
        super(context, 0, nombrePaises);
        this.context = context;
        this.nombrePaises = nombrePaises;
    }


    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        final LayoutInflater inflater = LayoutInflater.from(getContext());
        final View view = inflater.inflate(R.layout.cuerpo_lista_paises, null);


       // final View layout_referido = ((Activity)context).getWindow().getDecorView().findViewById(android.R.id.content);
        final TextView campo_nombre_pais = view.findViewById(R.id.campo_nombre_pais);
        final Button btn_info_pais = view.findViewById(R.id.btn_info_pais);

        campo_nombre_pais.setText(nombrePaises.get(position));

        campo_nombre_pais.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(context,ver_listado_monedas.class);
                i.putExtra("pais_seleccionado",campo_nombre_pais.getText().toString());
                context.startActivity(i);

                   //aqui deberia cerrar el context.

            }
        });
        btn_info_pais.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String codigo = "";
                String nombre = campo_nombre_pais.getText().toString();
                String capital = "";
                String habitantes = "";
                String fecha_union_euro = "";
                //  String textItemList = (listView.getItemAtPosition(position));
                //----------------------------------

                sqlite sqlite = new sqlite(context);

                SQLiteDatabase db = sqlite.getReadableDatabase();
                //Si hemos abierto correctamente la base de datos
                if (db != null) {

                    String sql = "SELECT * FROM paises_euro WHERE pais_nombre = '"+nombre+"'";
                    Cursor c = db.rawQuery(sql, null);

                    if (c != null) {
                        if (c.moveToFirst()) {
                            do {
                                codigo =  c.getString(0);
                                nombre =  c.getString(1);
                                capital = c.getString(2);
                                habitantes =  c.getString(3);
                                fecha_union_euro = c.getString(4);

                            } while (c.moveToNext());
                        }
                    }


                    //Cerramos la base de datos
                    db.close();
                    sqlite.close();

                    //----------------------------------


                    String mensaje = "COD PAIS: " + codigo + ".\n" +
                            "NOMBRE: " + nombre + ".\n" +
                            "CAPITAL: " + capital + ".\n" +
                            "HABITANTES: " + habitantes + ".\n" +
                            "FECHA UNION EURO: " + fecha_union_euro + ".";


                    AlertDialog.Builder builder = new AlertDialog.Builder(context);
                    builder.setMessage(mensaje)
                            .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    // FIRE ZE MISSILES!
                                }
                            }).show();

                }
            };


        });

        return view;
    }
}
    
asked by josanangel 04.06.2018 в 19:05
source

1 answer

1

It's because the finish() method does not belong to Context if not to Activity . To be able to call the finish() method you have to do a cast .

((Activity) context).finish(); 
    
answered by 04.06.2018 / 20:59
source