AutoCompleteTextView在从数据库添加数据时无法正常工作

时间:2016-01-28 11:59:29

标签: android sqlite android-sqlite autocompletetextview

我有一个 AutoCompleteTextView ,当我像这样静态地定义我的字符串时它可以正常工作..

private String Names[] = {"Hassan", "Usman", "Kristen Stewart"};

然后我这样做..

  ArrayAdapter<String> adapter = new ArrayAdapter<String>
            (this, android.R.layout.select_dialog_item, Names);
    //Getting the instance of AutoCompleteTextView

    catgNames.setThreshold(0);//will start working from 0 character
    catgNames.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView

当我尝试从 Sqlite 加载一些数据时,下拉建议不起作用。我正在做这样的事情..

  Categories = new String[100]
  int i =0;
  DatabaseHandler db = new DatabaseHandler(this);
    List<entries> entries1 = db.getCategories();
    for (entries cn : entries1) {
        Log.i("", cn.getCategories());

        Names[i] = cn.getCategories();
        i++;
    }

然后我定义适配器..

    ArrayAdapter<String> adapter = new ArrayAdapter<String>
            (this, android.R.layout.select_dialog_item, Names);
    //Getting the instance of AutoCompleteTextView

    catgNames.setThreshold(0);//will start working from 0 character
    catgNames.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView

这不起作用。 log-cat中没有错误或警告..

3 个答案:

答案 0 :(得分:0)

试试这个:

String[] Categories =[entries1.size];


int i =0;
  DatabaseHandler db = new DatabaseHandler(this);
    List<entries> entries1 = db.getCategories();
    for (entries cn : entries1) {
        Log.i("", cn.getCategories());

    Categories[i] = cn.getCategories();
    i++;
}

更改您的阈值:

catgNames.setThreshold(0);
像这样。

catgNames.setThreshold(1);

希望它有效。

答案 1 :(得分:0)

我想知道为什么在将名称添加到Names []时,会在ArrayAdapter中传递类别?它应该是ArrayName中的'Names'而不是'Categories',就像

一样
ArrayAdapter<String> adapter = new ArrayAdapter<String>
        (this, android.R.layout.select_dialog_item, Names);

或者您可以创建一个List,添加所有db列值并在ArrayAdapter中传递它们。希望它有所帮助!

答案 2 :(得分:0)

我已经解决了这个问题..

这是我正在做的错误..

Categories = new String[100]

这将创建一个100号的数组,我没有完全使用它。

现在......

DatabaseHandler db = new DatabaseHandler(this);
List<entries> entries1 = db.getCategories();
for (entries cn : entries1) {
    Log.i("", cn.getCategories());

    Names[i] = cn.getCategories();
    i++;
}

此代码只放置数据库中的值,其余值在Names数组中仍为null。

由于此autocompletetextview无法正常运行..

这是我如何解决它..

int i=0;
    DatabaseHandler db = new DatabaseHandler(this);
    List<entries> entries1 = db.getCategories();
    for (entries cn : entries1) {
        i++;
    }
    Names = new String[i];
    int j=0;
    List<entries> entries3 = db.getCategories();
    for (entries cn : entries3) {
        Names[j] = cn.getCategories();
        j++;
    }

找到此link

的解决方案