从SQLite数据库

时间:2018-01-12 10:47:43

标签: android sqlite autocomplete

我已经从我的数据库中为研究创建了一个自动填充文本,因此我的SQLite创建表格状态为:

private static final String CREATE_TABLE_QUESTION =
        "CREATE TABLE  " + TABLE_QUESTION + "("
        + KEY_ID_QUESTION + " INTEGER PRIMARY KEY autoincrement NOT NULL, "
        + KEY_QUESTION + " TEXT, "
        + KEY_PROFIL_CIBLE + " TEXT, "
        + KEY_PROFIL_WAITEDANSWER + " TEXT, "
        + KEY_REGLE + " TEXT, "
        + KEY_PLANACT + " TEXT, "
        + KEY_THEME + " TEXT"
        + ")";

我还创建了一个方法,在我的modelhelper中返回一个arraylist;

public ArrayList getQuestionForSearch() {
    ArrayList lst = new ArrayList();
    int zise = lst.size();
    Random rand = new Random();
    SQLiteDatabase db = this.getReadableDatabase();

    // Cursor c = db.rawQuery(" SELECT  DISTINCT " + KEY_QUESTION + " from " + TABLE_QUESTION+ " where " +KEY_QUESTION+ " LIKE '%" +search+ "%'" , null);
    Cursor c = db.rawQuery(" SELECT " + KEY_QUESTION + " from " + TABLE_QUESTION, null);

    if (c.moveToFirst()) {
        while (c.isAfterLast() == false) {
            String t1 = c.getString(c.getColumnIndex(KEY_QUESTION));
            lst.add(t1);
            c.moveToNext();
        }
    }
    return lst;
}

在我的活动中,我在.xlm文件中添加了自动填充文字:

<AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/txtcomplete"
        android:layout_width="wrap_content"
        android:hint="Tapez votre recherche ici"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        tools:layout_editor_absoluteY="0dp"
        tools:ignore="MissingConstraints" />

在我的.java类中,我创建了列表和一个适配器:

AutoCompleteTextView txt = (AutoCompleteTextView)findViewById(R.id.txtcomplete);

ModelHelper md = new ModelHelper(this);

String search= txt.getText().toString();
//getting list from the modelhelper
ArrayList<String> questions= md.getQuestionForSearch();
//arrayadapter
ArrayAdapter<String> myadap =   new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
//set the adapter to the autocomplete text
txt.setAdapter(myadap);

我验证了许多教程并且它的工作方式与此类似,但对我来说不一样,编译时没有错误,而在自动完成文本中键入时没有任何反复发生。

1 个答案:

答案 0 :(得分:1)

我发现它,问题在于适配器,我在适配器行中忘记了问题。

ArrayAdapter<String> myadap =   new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);

通过

ArrayAdapter<String> myadap =   new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,question);