Search code examples
javaandroidsqliteandroid-sqliteautocompletetextview

Error: cannot infer type arguments for ArrayAdapter<> in AutoCompleteTextView


I'm having trouble filling my auto_Owner variable in the RegisterLoan.java file with owner name records made in a query in the Names_owner() function which is located in the Connection.java file.

The problem is located in this line.

error: cannot infer type arguments for ArrayAdapter<>
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, Field_owner);
  • activity_register_loan.xml
          <AutoCompleteTextView
            android:id="@+id/auto_Owner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLength="30"
            android:layout_margin="5dp"
            android:completionThreshold="1"
            android:inputType="textCapWords|textCapSentences"
            android:hint="Enter the owner's name" />
  • Connection.java
public class Connection extends SQLiteOpenHelper {

    private static int version = 1;
    private static String base = "book_app";
    private SQLiteDatabase SQLiteBD;

    // Table owner
    private static final String table_owner = "owner";
    public static final String id_owner = "id_owner";
    public static final String name_owner = "name_owner";
    public static final String email_owner = "email_owner";
    public static final String telephone_owner = "telephone_owner";
    
    public Connection(@Nullable Context context){
        super(context, base, null, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + table_owner +
                "(" + id_owner + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
                + name_owner + " TEXT NOT NULL,"
                + email_owner + " TEXT NOT NULL,"
                + telephone_owner + " TEXT NOT NULL);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }
        
    public Cursor Names_owner() {
        SQLiteDatabase db = this.getReadableDatabase();
        String query = "SELECT " + name_owner + " FROM " + table_owner + " ORDER BY " + id_owner;
        Cursor cursor = db.rawQuery(query, null);
        return cursor;
    }
}
  • RegisterOwner.java
public class RegisterOwner extends AppCompatActivity {
    
    AutoCompleteTextView auto_Owner;
    Cursor Field_owner;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_loan);
        
        autoOwner = findViewById(R.id.autoOwner);
       
        Connection connection = new Connection(this);
        Field_owner = connection.Names_owner();
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, Field_owner);
        autoDono.setAdapter(adapter);
        
    }
}

Solution

  • You must pass an ArrayList of strings as the 3d argument of the ArrayAdapter constructor, but you pass Field_owner which is a Cursor object.

    You must loop through the cursor and fill a list with its values and then pass that list to the constructor:

    Field_owner = connection.Names_owner();
    ArrayList<String> names = new ArrayList<>();
    while (Field_owner.moveToNext()) {
        names.add(Field_owner.getString(0));
    }
    Field_owner.close();
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, names);
    autoDono.setAdapter(adapter);