我对AutoCompleteTextView
有疑问。我有ListView
由AutoCompleteTextView和EditText组成的数字输入,我可以修改它以动态添加行。第一行一切正常,但是当我添加第二行并尝试输入AutoCompleteTextView时,键盘失去焦点,我无法输入任何内容。这是我使用此列表视图的类:
public class AddMealActivity extends Activity {
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
EditText mealNameTextView;
ListView mealPartsListView;
Meal meal;
MealPartCustomAdapter mealPartsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_meal);
mealNameTextView = (EditText)findViewById(R.id.mealNameTextView);
mealPartsListView = (ListView)findViewById(R.id.mealPartsListView);
meal = new Meal();
mealPartsAdapter = new MealPartCustomAdapter(this, meal.mealParts);
mealPartsListView.setAdapter(mealPartsAdapter);
}
public void onOkMealClick(View view) {
meal.mealName = mealNameTextView.getText().toString();
meal.mealParts = mealPartsAdapter._mealParts;
finish();
}
@Override
public void finish() {
Intent data = new Intent();
data.putExtra("Meal", meal);
setResult(RESULT_OK, data);
super.finish();
}
public void onAddMealPartButtonClick(View view) {
mealPartsAdapter.add(new MealPart());
}
private static class MealPartCustomAdapter extends BaseAdapter {
private ArrayList<MealPart> _mealParts;
private Context _context;
private LayoutInflater mInflater;
static class ViewHolder {
AutoCompleteTextView productNameTextView;
EditText foodQuantityEditText;
}
public void add(MealPart mealPart) {
_mealParts.add(mealPart);
notifyDataSetChanged();
}
public MealPartCustomAdapter(Context context, ArrayList<MealPart> mealParts) {
mInflater = LayoutInflater.from(context);
_mealParts = new ArrayList<MealPart>(mealParts);
_context = context;
}
public int getCount() {
return _mealParts.size();
}
public Object getItem(int position) {
return _mealParts.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null) {
convertView = mInflater.inflate(R.layout.add_meal_list_view_row, null);
holder = new ViewHolder();
holder.productNameTextView = (AutoCompleteTextView) convertView.
findViewById(R.id.mealPartAutoCompleteTextView);
holder.foodQuantityEditText = (EditText) convertView.
findViewById(R.id.mealQuantityEditText);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.foodQuantityEditText
.setText(Utilities.trimDecimalsToString(_mealParts.get(position).foodQuantity));
holder.foodQuantityEditText.addTextChangedListener(new FoodQuantityTextWatcher(position, holder.foodQuantityEditText));
//TODO here fetch real product names from web API
//TODO on item selected - pass to mealPart proudctId of chosen product
ArrayAdapter<String> productNamesAdapter = new ArrayAdapter<String>(_context, android.R.layout.simple_list_item_1, DATA);
holder.productNameTextView.setAdapter(productNamesAdapter);
holder.productNameTextView.addTextChangedListener(new ProductNameTextWatcher(position,holder.productNameTextView));
return convertView;
}
private class ProductNameTextWatcher implements TextWatcher {
private int index;
private AutoCompleteTextView edittext;
public ProductNameTextWatcher(int index, AutoCompleteTextView edittext) {
this.index = index;
this.edittext = edittext;
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
public void afterTextChanged(Editable s) { _mealParts.get(index).productName = s.toString(); }
public void setIndex(int newindex) { index = newindex; }
}
private class FoodQuantityTextWatcher implements TextWatcher {
private int index;
private EditText edittext;
public FoodQuantityTextWatcher(int index, EditText edittext) {
this.index = index;
this.edittext = edittext;
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
public void afterTextChanged(Editable s) {
try {
_mealParts.get(index).foodQuantity = Float.valueOf(s.toString());
}
catch(NumberFormatException ex) {
_mealParts.get(index).foodQuantity = 0.0f;
}
}
public void setIndex(int newindex) { index = newindex; }
}
当我尝试触摸设备上的字段时,logcat说:
12-31 03:04:11.300:E / SpannableStringBuilder(29494):SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度
我想补充说,失去焦点发生在我的三星Galaxy S3 GT-I9300 上,机载4.1.2。在 Genymotion 模拟器(模拟相同的手机)上,这不会发生。