将第一个元素添加到arraylist的默认项目

时间:2017-08-24 17:14:13

标签: android arraylist autocompletetextview

这里我有一个数组列表

  ArrayList<String> arrlist = new ArrayList<String>();
while (cursors.moveToNext()) {
            arrlist.add((cursors.getString(cursors.getColumnIndex("location"))));
            adapter1 = new ArrayAdapter<String>(
                    getBaseContext(),
                    R.layout.drop_list_item,
                    arrlist);

            autocompletetextview.setAdapter(adapter1);

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    prepareMyList();
}

private void prepareMyList() {
    ArrayList<String> arrlist = new ArrayList<String>();
    String selectQuerys = "select distinct location from tb_user where location like '%"+autoservice.getText().toString()+"%'";
    SQLiteDatabase dbs = sq.getReadableDatabase();
    Cursor cursors = dbs.rawQuery(selectQuerys, null);

    while (cursors.moveToNext()) {
        arrlist.add((cursors.getString(cursors.getColumnIndex("location"))));
        adapter1 = new ArrayAdapter<String>(
                getBaseContext(),
                R.layout.drop_list_item,
                arrlist);

        autoservice.setAdapter(adapter1);

    }
}
@Override
public void afterTextChanged(Editable s) {

}

如果我输入&#39; a&#39;在自动填充文本视图中,所有项目都以&#39; a&#39;将作为建议,同样适用于所有其他信件  我需要的是我需要一个默认项目&#34;自动&#34;我们首先在自动填充文本视图中输入任何字母

2 个答案:

答案 0 :(得分:0)

听起来你需要在循环光标之前添加自动检测到的位置。早些时候,我在onCreate假设,它将是:

ArrayList<String> arrlist = new ArrayList<String>();
arrlist.add("automatically detect");
while (cursors.moveToNext()) { //...

然后在prepareMyList

private void prepareMyList() {
  ArrayList<String> arrlist = new ArrayList<String>();
  arrlist.add("automatically detect");
  String selectQuerys = //...

这应该添加&#34;自动检测&#34;作为最初和搜索时的第一项。

注意:您应该考虑将部分代码移出while循环,以提高效率(并且可能减少错误)。您不需要每次都通过循环创建新的ArrayAdapter或设置适配器。你可以在循环结束后这样做一次。循环中所需要的只是:

while (cursors.moveToNext()) {
    arrlist.add((cursors.getString(cursors.getColumnIndex("location"))));
}
adapter1 = new ArrayAdapter<String>(
            getBaseContext(),
            R.layout.drop_list_item,
            arrlist);
autoservice.setAdapter(adapter1);

答案 1 :(得分:0)

ArrayList<String> arrlist = new ArrayList<String>();
arrlist.add("Default element");