android 中动态视图的自动完成
Autocomplete on a dynamic view in android
我已经创建了一个表单供用户添加到我的数据库中。我的表单的一部分可以有不同数量的字段,所以我创建了一个按钮来添加带有字段的自定义布局。
View child = getLayoutInflater().inflate(R.layout.ingredient_input, ll, false);
ll.addView(child);
到目前为止效果很好。
对于我添加的每个视图,都有一个将访问相同列表的 AutoCompleteTextView。我可以使用页面上已有的那个来工作:
acIng1 = (AutoCompleteTextView)findViewById(R.id.acIng1);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(
NewRecipeActivity.this,
android.R.layout.simple_list_item_1,
nameList
);
acIng1.setAdapter(adapter1);
但是我无法在我动态添加的任何视图中自动完成,因为我没有它们的 ID。关于如何 运行 在这些新字段上自动完成的任何想法?这是我的自定义视图:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editText1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<AutoCompleteTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" />
<TextView
android:id="@+id/tvIng1Unit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Unit" />
</LinearLayout>
您可以向您的布局添加一个 ID(您以编程方式添加的那个):
<AutoCompleteTextView
...android:id="@+id/auto"
/>
并在您的子视图(即您创建的视图)上使用 findViewById。
View child = getLayoutInflater().inflate(R.layout.ingredient_input, ll, false);
ll.addView(child);
child.findViewById(R.id.auto);
不用担心id不会混淆。因为您总是在不同的视图中搜索,即使它是相同的 ID。
简单的解决方案:在容器中找到所有 AutoCompleteTextView
并设置适配器。
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(
NewRecipeActivity.this,
android.R.layout.simple_list_item_1,
nameList
);//adapter
ViewGroup v= (ViewGroup)findViewById(R.id.LinearLayout1);//container
for(int i=0; i<((ViewGroup)v).getChildCount(); ++i) {
View nextChild = ((ViewGroup)v).getChildAt(i);
if (nextChild instanceof AutoCompleteTextView) {
((AutoCompleteTextView)nextChild).setAdapter(adapter1);
}
}
我已经创建了一个表单供用户添加到我的数据库中。我的表单的一部分可以有不同数量的字段,所以我创建了一个按钮来添加带有字段的自定义布局。
View child = getLayoutInflater().inflate(R.layout.ingredient_input, ll, false);
ll.addView(child);
到目前为止效果很好。
对于我添加的每个视图,都有一个将访问相同列表的 AutoCompleteTextView。我可以使用页面上已有的那个来工作:
acIng1 = (AutoCompleteTextView)findViewById(R.id.acIng1);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(
NewRecipeActivity.this,
android.R.layout.simple_list_item_1,
nameList
);
acIng1.setAdapter(adapter1);
但是我无法在我动态添加的任何视图中自动完成,因为我没有它们的 ID。关于如何 运行 在这些新字段上自动完成的任何想法?这是我的自定义视图:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editText1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<AutoCompleteTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" />
<TextView
android:id="@+id/tvIng1Unit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Unit" />
</LinearLayout>
您可以向您的布局添加一个 ID(您以编程方式添加的那个):
<AutoCompleteTextView
...android:id="@+id/auto"
/>
并在您的子视图(即您创建的视图)上使用 findViewById。
View child = getLayoutInflater().inflate(R.layout.ingredient_input, ll, false);
ll.addView(child);
child.findViewById(R.id.auto);
不用担心id不会混淆。因为您总是在不同的视图中搜索,即使它是相同的 ID。
简单的解决方案:在容器中找到所有 AutoCompleteTextView
并设置适配器。
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(
NewRecipeActivity.this,
android.R.layout.simple_list_item_1,
nameList
);//adapter
ViewGroup v= (ViewGroup)findViewById(R.id.LinearLayout1);//container
for(int i=0; i<((ViewGroup)v).getChildCount(); ++i) {
View nextChild = ((ViewGroup)v).getChildAt(i);
if (nextChild instanceof AutoCompleteTextView) {
((AutoCompleteTextView)nextChild).setAdapter(adapter1);
}
}