4

我正在使用特殊的 autocompletetextview 来提供数千条街道的建议。由于这个庞大的元素列表,我必须根据第一个输入字母即时清除和填充适配器。例如,当用户输入“b”时,适配器会填充以“b”开头的所有街道。这适用于所有 Android 版本。今天一位用户告诉我,Android 2.3.5 三星 Galaxy Ace 有问题。

当他开始输入字母时,没有带有建议的下拉菜单。当他输入“b”时,它只建议由两个名称组成的街道,而第二个名称以“b”开头。例如:他为“b”得到的建议是“Am Bahnhof”或“Alte Bahnstraße”。似乎有一种模式,但我无法解释这是如何发生的,我无法在任何模拟器或真实设备上重现这一点(而且没有 2.3.5 模拟器)。

下面是代码。仅供参考:我必须在 beforeTextChange 中清除,否则当我很快删除字段中的输入时应用程序会崩溃。显然它不能足够快地交换列表。而且我需要已经添加的变量,否则当我将输入删除到第一个字母时,它会添加两次列表。

真正奇怪的是:自从第一次实现以来,我从未改变过这部分,用户告诉我它在第一个版本中工作。但是突然之间它不再起作用了。编译的代码是否可能以某种方式损坏?有任何想法吗?

当有人可以告诉我如何重现此问题时,我也会接受解决方案。

调用:

autoCompleteTextView.addTextChangedListener(new StreetTextWatcher(adapter));

街道阵列适配器:

public class StreetArrayAdapter extends ArrayAdapter<String> {

    public StreetArrayAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

    public void addAll(String[] streets) {
        for (String street : streets) {
            add(street);
        }
    }
}

StreetTextWatcher:

public class StreetTextWatcher implements TextWatcher {

    private final StreetArrayAdapter adapter;
    private boolean alreadyAdded = false;

    public StreetTextWatcher(StreetArrayAdapter adapter) {
        this.adapter = adapter;
    }

    @Override
    public void afterTextChanged(Editable s) {
        //not used
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        if (s.length() < 1)  {
            adapter.clear();

            alreadyAdded = false;
        }
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {           
        if (s.length() == 1) {
            populateAdapter(s);

            alreadyAdded = true;
        }
    }

    private void populateAdapter(CharSequence s) {  
        if (s.toString().toLowerCase(Locale.GERMAN).startsWith("a") && !alreadyAdded)  {
            adapter.addAll(StreetNames.STREETS_A);
        }

        if (s.toString().toLowerCase(Locale.GERMAN).startsWith("ä") && !alreadyAdded)  {
            adapter.addAll(StreetNames.STREETS_A);
        }

        if (s.toString().toLowerCase(Locale.GERMAN).startsWith("b") && !alreadyAdded)  {
            adapter.addAll(StreetNames.STREETS_B);
        }

        if (s.toString().toLowerCase(Locale.GERMAN).startsWith("c") && !alreadyAdded)  {
            adapter.addAll(StreetNames.STREETS_C);
        }

        if (s.toString().toLowerCase(Locale.GERMAN).startsWith("d") && !alreadyAdded)  {
            adapter.addAll(StreetNames.STREETS_D);
        }

        if (s.toString().toLowerCase(Locale.GERMAN).startsWith("e") && !alreadyAdded)  {
            adapter.addAll(StreetNames.STREETS_E);
        }

        if (s.toString().toLowerCase(Locale.GERMAN).startsWith("f") && !alreadyAdded)  {
            adapter.addAll(StreetNames.STREETS_F);
        }

        if (s.toString().toLowerCase(Locale.GERMAN).startsWith("g") && !alreadyAdded)  {
            adapter.addAll(StreetNames.STREETS_G);
        }

        if (s.toString().toLowerCase(Locale.GERMAN).startsWith("h") && !alreadyAdded)  {
            adapter.addAll(StreetNames.STREETS_H);
        }

        if (s.toString().toLowerCase(Locale.GERMAN).startsWith("i") && !alreadyAdded)  {
            adapter.addAll(StreetNames.STREETS_I);
        }

        if (s.toString().toLowerCase(Locale.GERMAN).startsWith("j") && !alreadyAdded)  {
            adapter.addAll(StreetNames.STREETS_J);
        }

        if (s.toString().toLowerCase(Locale.GERMAN).startsWith("k") && !alreadyAdded)  {
            adapter.addAll(StreetNames.STREETS_K);
        }

        if (s.toString().toLowerCase(Locale.GERMAN).startsWith("l") && !alreadyAdded)  {
            adapter.addAll(StreetNames.STREETS_L);
        }

        if (s.toString().toLowerCase(Locale.GERMAN).startsWith("m") && !alreadyAdded)  {
            adapter.addAll(StreetNames.STREETS_M);
        }

        if (s.toString().toLowerCase(Locale.GERMAN).startsWith("n") && !alreadyAdded)  {
            adapter.addAll(StreetNames.STREETS_N);
        }

        if (s.toString().toLowerCase(Locale.GERMAN).startsWith("o") && !alreadyAdded)  {
            adapter.addAll(StreetNames.STREETS_O);
        }

        if (s.toString().toLowerCase(Locale.GERMAN).startsWith("ö") && !alreadyAdded)  {
            adapter.addAll(StreetNames.STREETS_O);
        }

        if (s.toString().toLowerCase(Locale.GERMAN).startsWith("p") && !alreadyAdded)  {
            adapter.addAll(StreetNames.STREETS_P);
        }

        if (s.toString().toLowerCase(Locale.GERMAN).startsWith("q") && !alreadyAdded)  {
            adapter.addAll(StreetNames.STREETS_Q);
        }

        if (s.toString().toLowerCase(Locale.GERMAN).startsWith("r") && !alreadyAdded)  {
            adapter.addAll(StreetNames.STREETS_R);
        }

        if (s.toString().toLowerCase(Locale.GERMAN).startsWith("s") && !alreadyAdded)  {
            adapter.addAll(StreetNames.STREETS_S);
        }

        if (s.toString().toLowerCase(Locale.GERMAN).startsWith("t") && !alreadyAdded)  {
            adapter.addAll(StreetNames.STREETS_T);
        }

        if (s.toString().toLowerCase(Locale.GERMAN).startsWith("u") && !alreadyAdded)  {
            adapter.addAll(StreetNames.STREETS_U);
        }

        if (s.toString().toLowerCase(Locale.GERMAN).startsWith("ü") && !alreadyAdded)  {
            adapter.addAll(StreetNames.STREETS_U);
        }

        if (s.toString().toLowerCase(Locale.GERMAN).startsWith("v") && !alreadyAdded)  {
            adapter.addAll(StreetNames.STREETS_V);
        }

        if (s.toString().toLowerCase(Locale.GERMAN).startsWith("w") && !alreadyAdded)  {
            adapter.addAll(StreetNames.STREETS_W);
        }

        if (s.toString().toLowerCase(Locale.GERMAN).startsWith("y") && !alreadyAdded)  {
            adapter.addAll(StreetNames.STREETS_Y);
        }

        if (s.toString().toLowerCase(Locale.GERMAN).startsWith("z") && !alreadyAdded)  {
            adapter.addAll(StreetNames.STREETS_Z);
        }
    }
}
4

2 回答 2

2

我对优化发表了评论......但由于我正在粘贴代码......我想保留格式,所以我粘贴为答案,即使这不能回答你的问题。

这是我编写代码以使其更容易更改/添加/维护的方式...

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) 
{           
    if (s.length() == 1) 
    {
        if (!alreadyAdded)
        {
            alreadyAdded = true;
            populateAdapter(s);
        }
    }
}

private void populateAdapter(CharSequence s) {  
    Object arr[][] = 
    {{"a", StreetNames.STREETS_A },
     {"ä", StreetNames.STREETS_A },
     {"b", StreetNames.STREETS_B },
     {"c", StreetNames.STREETS_C },
     {"d", StreetNames.STREETS_D },
     {"e", StreetNames.STREETS_E },
     {"f", StreetNames.STREETS_F },
     {"g", StreetNames.STREETS_G },
     {"h", StreetNames.STREETS_H },
     {"i", StreetNames.STREETS_I },
     {"j", StreetNames.STREETS_J },
     {"k", StreetNames.STREETS_K },
     {"l", StreetNames.STREETS_L },
     {"m", StreetNames.STREETS_M },
     {"n", StreetNames.STREETS_N },
     {"o", StreetNames.STREETS_O },
     {"ö", StreetNames.STREETS_O },
     {"p", StreetNames.STREETS_P },
     {"q", StreetNames.STREETS_Q },
     {"r", StreetNames.STREETS_R },
     {"s", StreetNames.STREETS_S },
     {"t", StreetNames.STREETS_T },
     {"u", StreetNames.STREETS_U },
     {"ü", StreetNames.STREETS_U },
     {"v", StreetNames.STREETS_V },
     {"w", StreetNames.STREETS_W },
     {"y", StreetNames.STREETS_Y },
     {"z", StreetNames.STREETS_Z }};

    for (int i=0; i<arr.length; i++)
    {
        if (s.toString().toLowerCase(Locale.GERMAN).startsWith(arr[i][0]))  {
            adapter.addAll(arr[i][1]);
            break;
        }
    }
}
于 2013-02-18T04:50:28.883 回答
-4

将以下代码用于自动完成 TextView

MainActivity.java

public class MainActivity extends Activity {

AutoCompleteTextView autoCompleteTextView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.AndroidBooks);

    StreetArrayAdapter adapter = new StreetArrayAdapter(MainActivity.this,
            android.R.layout.simple_gallery_item);
    autoCompleteTextView.setThreshold(1);
    autoCompleteTextView.setAdapter(adapter);
    autoCompleteTextView.addTextChangedListener(new StreetTextWatcher(
            autoCompleteTextView, adapter));

}
}

主要的.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <AutoCompleteTextView
        android:id="@+id/AndroidBooks"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:inputType="text">
    </AutoCompleteTextView>
</LinearLayout>

StreetArrayAdapter.java

public class StreetArrayAdapter extends ArrayAdapter<String> {

String TAG = getClass().getSimpleName();

public StreetArrayAdapter(Context context, int textViewResourceId) {
    super(context, textViewResourceId);
}

public void addAll(String[] streets) {
    for (String street : streets) {
        Log.v(TAG, "street = " + street);
        add(street);
    }
}

@Override
public void insert(String object, int index) {
    super.insert(object, index);
}

@Override
public void notifyDataSetChanged() {
    super.notifyDataSetChanged();
}

@Override
public void setDropDownViewResource(int resource) {
    super.setDropDownViewResource(resource);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    return super.getView(position, convertView, parent);

}


}

StreetTextWatcher.java

public class StreetTextWatcher implements TextWatcher {

private final StreetArrayAdapter adapter;
private boolean alreadyAdded = false;

String TAG = getClass().getSimpleName();
AutoCompleteTextView autoCompleteTextView;

public StreetTextWatcher(AutoCompleteTextView autoCTV,StreetArrayAdapter adapter) {
    this.adapter = adapter;
    autoCompleteTextView = autoCTV;
}

@Override
public void afterTextChanged(Editable s) {
    // not used
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
        int after) {
    if (s.length() < 1) {
        adapter.clear();

        alreadyAdded = false;
    }
}

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

    Log.v(TAG, "char ---> = "+ s);
    if (s.length() == 1) {
        populateAdapter(s);

        alreadyAdded = true;
    }
}


private void populateAdapter(CharSequence s) {
    if (s.toString().toLowerCase(Locale.GERMAN).startsWith("a")
            && !alreadyAdded) {
        Log.v(TAG, "inside A");
        adapter.addAll(StreetNames.STREETS_A);
    }

    if (s.toString().toLowerCase(Locale.GERMAN).startsWith("ä")
            && !alreadyAdded) {
        adapter.addAll(StreetNames.STREETS_A);
    }

    if (s.toString().toLowerCase(Locale.GERMAN).startsWith("b")
            && !alreadyAdded) {
        adapter.addAll(StreetNames.STREETS_B);
    }

    if (s.toString().toLowerCase(Locale.GERMAN).startsWith("c")
            && !alreadyAdded) {
        adapter.addAll(StreetNames.STREETS_C);
    }

    if (s.toString().toLowerCase(Locale.GERMAN).startsWith("d")
            && !alreadyAdded) {
        adapter.addAll(StreetNames.STREETS_D);
    }

    if (s.toString().toLowerCase(Locale.GERMAN).startsWith("e")
            && !alreadyAdded) {
        adapter.addAll(StreetNames.STREETS_E);
    }

    if (s.toString().toLowerCase(Locale.GERMAN).startsWith("f")
            && !alreadyAdded) {
        adapter.addAll(StreetNames.STREETS_F);
    }

    if (s.toString().toLowerCase(Locale.GERMAN).startsWith("g")
            && !alreadyAdded) {
        adapter.addAll(StreetNames.STREETS_G);
    }

    if (s.toString().toLowerCase(Locale.GERMAN).startsWith("h")
            && !alreadyAdded) {
        adapter.addAll(StreetNames.STREETS_H);
    }

    if (s.toString().toLowerCase(Locale.GERMAN).startsWith("i")
            && !alreadyAdded) {
        adapter.addAll(StreetNames.STREETS_I);
    }

    if (s.toString().toLowerCase(Locale.GERMAN).startsWith("j")
            && !alreadyAdded) {
        adapter.addAll(StreetNames.STREETS_J);
    }

    if (s.toString().toLowerCase(Locale.GERMAN).startsWith("k")
            && !alreadyAdded) {
        adapter.addAll(StreetNames.STREETS_K);
    }

    if (s.toString().toLowerCase(Locale.GERMAN).startsWith("l")
            && !alreadyAdded) {
        adapter.addAll(StreetNames.STREETS_L);
    }

    if (s.toString().toLowerCase(Locale.GERMAN).startsWith("m")
            && !alreadyAdded) {
        adapter.addAll(StreetNames.STREETS_M);
    }

    if (s.toString().toLowerCase(Locale.GERMAN).startsWith("n")
            && !alreadyAdded) {
        adapter.addAll(StreetNames.STREETS_N);
    }

    if (s.toString().toLowerCase(Locale.GERMAN).startsWith("o")
            && !alreadyAdded) {
        adapter.addAll(StreetNames.STREETS_O);
    }

    if (s.toString().toLowerCase(Locale.GERMAN).startsWith("ö")
            && !alreadyAdded) {
        adapter.addAll(StreetNames.STREETS_O);
    }

    if (s.toString().toLowerCase(Locale.GERMAN).startsWith("p")
            && !alreadyAdded) {
        adapter.addAll(StreetNames.STREETS_P);
    }

    if (s.toString().toLowerCase(Locale.GERMAN).startsWith("q")
            && !alreadyAdded) {
        adapter.addAll(StreetNames.STREETS_Q);
    }

    if (s.toString().toLowerCase(Locale.GERMAN).startsWith("r")
            && !alreadyAdded) {
        adapter.addAll(StreetNames.STREETS_R);
    }

    if (s.toString().toLowerCase(Locale.GERMAN).startsWith("s")
            && !alreadyAdded) {
        adapter.addAll(StreetNames.STREETS_S);
    }

    if (s.toString().toLowerCase(Locale.GERMAN).startsWith("t")
            && !alreadyAdded) {
        adapter.addAll(StreetNames.STREETS_T);
    }

    if (s.toString().toLowerCase(Locale.GERMAN).startsWith("u")
            && !alreadyAdded) {
        adapter.addAll(StreetNames.STREETS_U);
    }

    if (s.toString().toLowerCase(Locale.GERMAN).startsWith("ü")
            && !alreadyAdded) {
        adapter.addAll(StreetNames.STREETS_U);
    }

    if (s.toString().toLowerCase(Locale.GERMAN).startsWith("v")
            && !alreadyAdded) {
        adapter.addAll(StreetNames.STREETS_V);
    }

    if (s.toString().toLowerCase(Locale.GERMAN).startsWith("w")
            && !alreadyAdded) {
        adapter.addAll(StreetNames.STREETS_W);
    }

    if (s.toString().toLowerCase(Locale.GERMAN).startsWith("y")
            && !alreadyAdded) {
        adapter.addAll(StreetNames.STREETS_Y);
    }

    if (s.toString().toLowerCase(Locale.GERMAN).startsWith("z")
            && !alreadyAdded) {
        adapter.addAll(StreetNames.STREETS_Z);
    }

    autoCompleteTextView.setAdapter(adapter);
}

}

StreetNames.java

public class StreetNames {
 public static String[] STREETS_A = {"Apple", "Ant", "ana","alia"};
 public static String[] STREETS_B = {"Ball", "Baby","banana","bear"};
 public static String[] STREETS_C = {"Cat", "Coffee", "cherry", "carrot"};
 public static String[] STREETS_D = {"Doll", "Dady", "dj", "don"};
 public static String[] STREETS_E = {"Elephant", "Eye", "earing", "easy"};
 /*
  .
  .
  */
 public static String[] STREETS_Z = {"Zebra", "Zero", "zzz", "zoya"};
}

如果您有任何疑问,请告诉我...

于 2013-02-11T11:19:38.383 回答