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

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

我在 RegisterLoan.java 文件中填充我的 auto_Owner 变量时遇到问题,该变量是在 Names_owner() 函数中查询的所有者姓名记录,该函数位于 [=17] =] 文件.

问题出在这一行。

error: cannot infer type arguments for ArrayAdapter<>
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, Field_owner);
          <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" />
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;
    }
}
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);
        
    }
}

您必须传递 ArrayList 个字符串作为 ArrayAdapter 构造函数的 3d 参数,但您传递的 Field_owner 是一个 Cursor 对象。

您必须遍历游标并用它的值填充一个列表,然后将该列表传递给构造函数:

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);
    

我发现你的代码有问题,字段所有者是返回字符串对象的游标类型,但你的数组适配器需要一个字符串值,你可以:

  • 设置将字段所有者作为字符串参数传递给数组适配器

ArrayAdapter 适配器 = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, Field_owner.toString());

  • 单独创建一个字符串值并使用它来解析游标的值

String text = String.parse(游标的值);

ArrayAdapter 适配器 = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, text);