我正在使用特殊的autocompletetextview来获取成千上万条街道的建议。由于这个巨大的元素列表,我必须根据第一个输入字母动态清除和填充适配器。例如,当用户输入“b”时,适配器将填充所有以“b”开头的街道。这适用于所有Android版本。今天有一位用户告诉我Android 2.3.5中存在问题,三星Galaxy Ace。
当他开始输入信件时,没有建议的下拉列表。 当他输入“b”时,它只表示由两个名字组成的街道,而第二个名字以“b”开头。例如:他为“b”获得的建议是“Am Bahnhof”或“AlteBahnstraße”。似乎有一种模式,但我无法解释这是如何发生的,我无法重现这一点,无论是在任何模拟器中还是在真实设备上(此外,没有2.3.5模拟器)。
以下是代码。仅供参考:我必须在beforeTextChange中清除,否则当我在字段中快速删除输入时应用程序将崩溃。显然它不能足够快地交换列表。我需要变量alreadyAdded,否则当我将输入删除到第一个字母时,它会添加两次列表。
真正奇怪的是:自从第一次实施以来,我从未改变过这部分,用户告诉我它在第一个版本中有效。但突然之间,它不再起作用了。编译的代码是否有可能腐败?有什么想法吗?
当有人告诉我如何重现这个时,我也会接受一个解决方案。
调用:
autoCompleteTextView.addTextChangedListener(new StreetTextWatcher(adapter));
StreetArrayAdapter:
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);
}
}
}
答案 0 :(得分: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;
}
}
}
答案 1 :(得分:-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));
}
}
main.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"};
}
如果您有任何疑问,请告诉我......