【问题标题】:AutocompleteText loaded from SQLite database从 SQLite 数据库加载的 AutocompleteText
【发布时间】:2018-06-21 19:41:48
【问题描述】:

我已经为我的数据库中的一项研究制作了一个自动完成文本,所以我的 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"
        + ")";

我还在我的模型助手中创建了一个返回数组列表的方法;

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

我验证了很多教程,它的工作原理是这样的,但对我来说不一样,编译时没有错误,在输入自动完成文本时没有任何反应。

【问题讨论】:

  • questions 的大小是多少??检查questions.size()
  • 没什么,我从其他方法复制代码忘记删除了。

标签: android sqlite autocomplete


【解决方案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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-09
    • 2017-10-09
    • 2012-01-19
    • 1970-01-01
    • 2018-11-12
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    相关资源
    最近更新 更多