无法解析构造 'arrayadapter(android.widget.adaptervie

2019-10-19 09:40发布

这是我的问题:

public class captura_actividad extends Activity {

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmdd");
String date = dateFormat.format(new Date() );
private final String ruta_fotos = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/CivilControl/" + date + "/";
private File file = new File(ruta_fotos);
private Button boton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.captura_actividad);

    final ImageButton boton_categoria_actividad = (ImageButton) findViewById(R.id.crearCapturaActividad);
    boton_categoria_actividad.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent boton_intent_categoria_actividad = new Intent (captura_actividad.this, categoria_actividad.class);
            startActivity(boton_intent_categoria_actividad);
        }
    });

    // MANEJO SPINNER O SELECT CATEGORIA

    Spinner spinnerCategoria = (Spinner) findViewById(R.id.spinnerCategoria);

   // String[] dbNombreCategoria = {"a","b","c","d","e","f", "g", "h"};
    crearBD usdbh = new crearBD (this, "civilControl", null, 1);
    SQLiteDatabase db;
    db = usdbh.getWritableDatabase();

    String[] campos = new String[] {"_id","nombreCategoria"};
    //String[] args = new String[] {""};

    //Se ejecuta la consulta
    Cursor c;
    c = db.query("categoria", campos, null, null,null, null, null);


    final ArrayList<String> dbNombreCategoria = new ArrayList<String>();

    String categoria;

    if(c.getCount()!=0){
        if(c.moveToFirst()){
            do{

                categoria = c.getString(1);
                dbNombreCategoria.add(categoria);

            }while(c.moveToNext());
        }
    }
    else{
        dbNombreCategoria.add("No existen categorias");
    }


    spinnerCategoria.setAdapter(new ArrayAdapter<String>(this, simple_spinner_item, dbNombreCategoria));
    spinnerCategoria.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id)
        {
           /*******************************************/
            Spinner spinnerActividad = (Spinner) findViewById(R.id.spinnerActividad);

            // String[] dbNombreCategoria = {"a","b","c","d","e","f", "g", "h"};


            final ArrayList<String> dbSpinnerActividad = new ArrayList<String>();

            dbSpinnerActividad.add("si");
            dbSpinnerActividad.add("ok");


            spinnerActividad.setAdapter(new ArrayAdapter<String>(this, simple_spinner_item, dbSpinnerActividad));
            spinnerActividad.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id)
                {

                }

                @Override
                public void onNothingSelected(AdapterView<?> parent)
                {
                    // vacio

                }
            });
           /*******************************************/
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent)
        {
            // vacio

        }
    });

    // FIN MANEJO SPINNER O SELECT CATEGORIA

    // MANEJO SPINNER O SELECT ACTIVIDAD

    Spinner spinnerActividad = (Spinner) findViewById(R.id.spinnerActividad);
    String[] valoresActividad = {"a","b","c","d","e","f", "g", "h"};
    spinnerActividad.setAdapter(new ArrayAdapter<String>(this, simple_spinner_item, valoresActividad));
    spinnerActividad.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id)
        {
            Toast.makeText(adapterView.getContext(), (String) adapterView.getItemAtPosition(position), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent)
        {
            // vacio

        }
    });

    // FIN MANEJO SPINNER O SELECT ACTIVIDAD

    // MANEJO CAMARA
    boton = (Button) findViewById(R.id.btnTomaFoto);
    //Si no existe crea la carpeta donde se guardaran las fotos
    file.mkdirs();
    //accion para el boton
    boton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String file = ruta_fotos + getCode() + ".jpg";
            File mi_foto = new File( file );
            try {
                mi_foto.createNewFile();
            } catch (IOException ex) {
                Log.e("ERROR ", "Error:" + ex);
            }
            //
            Uri uri = Uri.fromFile( mi_foto );
            //Abre la camara para tomar la foto
            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            //Guarda imagen
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
            //Retorna a la actividad
            startActivityForResult(cameraIntent, 0);
        }

    });
    //====== FIN MANEJO CAMARA ======
}

/**
 * Metodo privado que genera un codigo unico segun la hora y fecha del sistema
 * @return photoCode
 * */
@SuppressLint("SimpleDateFormat")
private String getCode()
{
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
    String date = dateFormat.format(new Date() );
    String photoCode = "pic_" + date;
    return photoCode;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.captura, menu);
    return true;
}

}

我有进口的:

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import static android.R.layout.*;

而我得到这个错误:

无法解析构造 'arrayadapter(android.widget.adapterview.onitemselectedlistener,INT,java.util.ArrayList中的)'

在这条线

spinnerActividad.setAdapter(new ArrayAdapter<String>(this, simple_spinner_item, dbSpinnerActividad));

Answer 1:

因为this在这里是android.widget.adapterview.onitemselectedlistener.this ,但ArrayAdapter需要的Context ,其spinnerActividad 。在这样跑,你可以用这个说法来取代入错行:

spinnerActividad.setAdapter(new ArrayAdapter<String>(captura_actividad.this, simple_spinner_item, dbSpinnerActividad));


Answer 2:

试图取代

spinnerActividad.setAdapter(new ArrayAdapter<String>(this, simple_spinner_item, dbSpinnerActividad));

对于

spinnerActividad.setAdapter(new ArrayAdapter<String>(getApplicationContext(), simple_spinner_item, dbSpinnerActividad));


文章来源: cannot resolve constructor 'arrayadapter(android.widget.adapterview.onitemselectedlistener, int, java.util.arraylist)'