我有一个ListView
,其中我使用AutoCompleteTextView和EditText填充它。用户可以编程方式添加新行并编辑它们。但是,当我添加两个或多个行时,我无法在第二个/第三个等AutoCompleteTextView中输入文本-当我触摸TextView时,键盘出现,但我无法输入任何内容,因为AutoCompleteTextView失去了焦点。以下是我的适配器getView和具有行设计的xml。
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null) {
convertView = mInflater.inflate(R.layout.add_meal_activity_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;
}
这是行定义。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="horizontal"
android:padding="5dp"
android:focusable="true"
android:focusableInTouchMode="true" >
<AutoCompleteTextView
android:id="@+id/mealPartAutoCompleteTextView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:completionThreshold="2"
android:inputType="textNoSuggestions"
android:textSize="15sp"
style="@style/ListTextView" />
<EditText android:id="@+id/mealQuantityEditText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:inputType="number"
android:textSize="15sp"
style="@style/ListTextView"
/>
</LinearLayout>
这里是一个列表的行被填充的活动。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
style="@style/ListViewStyle"
android:focusableInTouchMode="true"
android:focusable="true">
<EditText android:id="@+id/mealNameTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/mealNameTextViewHint"
style="@style/TitleTextViewStyle" />
<ListView android:id="@+id/mealPartsListView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<ImageButton
android:id="@+id/okMealButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="left"
android:src="@drawable/ic_action_accept"
android:contentDescription="@string/addMealPartButtonContentDescription"
android:onClick="onOkMealClick"
android:background="?android:selectableItemBackground"
android:padding="5dp">
</ImageButton>
<ImageButton
android:id="@+id/addMealPartButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="left"
android:src="@drawable/ic_action_new"
android:contentDescription="@string/addMealPartButtonContentDescription"
android:onClick="onAddMealPartButtonClick"
android:padding="5dp"
android:background="?android:selectableItemBackground">
</ImageButton>
<ImageButton
android:id="@+id/addFavouriteMeal"
android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="left"
android:src="@drawable/ic_action_favorite"
android:contentDescription="@string/addMealPartButtonContentDescription"
android:onClick="onAddMealPartButtonClick"
android:padding="5dp"
android:background="?android:selectableItemBackground">
</ImageButton>
</LinearLayout>
</LinearLayout>
更新
我查看了LogCat,在点击AutoComplete时,无法获取焦点时会记录类似以下内容的日志:
01-07 12:35:08.685: E/SpannableStringBuilder(29361): SPAN_EXCLUSIVE_EXCLUSIVE 跨度不能具有零长度