我在获取 android 自动完成文本视图以显示过滤的列表项以及列表(如果使用而不是自动完成文本视图)时遇到问题,根本不显示。
下面的代码片段显示了我在两种情况下使用片段的代码:
private boolean isAuto = false;
private void switchView() {
// TODO Auto-generated method stub
FragmentManager fm = getSupportFragmentManager();
Fragment f = null;
Bundle b = new Bundle();
b.putSerializable("from", fromList);
b.putSerializable("to", toList);
if(isAuto){
if(autoCompleteFragment == null) {
autoCompleteFragment = new AutoCompleteFragment();
}
f = autoCompleteFragment;
} else {
if(listClassicFragment == null) {
listClassicFragment = new ListClassicFragment();
}
f = listClassicFragment;
}
f.setArguments(b);
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_content, f);
ft.commit();
}
这是两个视图的自定义适配器:
public static class EntryArrayAdapter extends ArrayAdapter<AbstractMap.SimpleImmutableEntry<String,String>> {
List<AbstractMap.SimpleImmutableEntry<String,String>> data;
public EntryArrayAdapter(Context context, int textViewResourceId,
List<AbstractMap.SimpleImmutableEntry<String, String>> objects) {
super(context, textViewResourceId, objects);
data = objects;
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView t = new TextView(getContext());
t.setText(data.get(position).getValue());
t.setTag(data.get(position).getKey());
Log.d(TAG, "" + t.getText() + ":" + t.getTag());
return t;
}
}
这是片段活动布局:
<?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"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="20dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/from_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/from_label"
android:textStyle="bold"
android:typeface="serif"
/>
<TextView
android:id="@+id/to_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/to_label"
android:textStyle="bold"
android:typeface="serif" />
</LinearLayout>
<FrameLayout
android:id="@+id/fragment_content"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/value_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/value_label"
android:textStyle="bold"
android:typeface="serif"
>
</TextView>
<EditText
android:id="@+id/value"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal"
android:layout_marginLeft="20dp"
android:inputType="numberDecimal"
android:maxLength="20"
android:layout_weight="1"
/>
<TextView
android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/result" android:layout_weight="1"
android:textStyle="bold"
android:typeface="serif"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginLeft="10dp"
>
</TextView>
</LinearLayout>
<Button
android:id="@+id/submitButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="50dp"
android:text="@string/submit_label" />
</LinearLayout>
</LinearLayout>
列表视图片段布局:
<?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="0dp"
android:layout_weight=".5"
android:orientation="horizontal" >
<ListView
android:id="@+id/from_list"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="@drawable/border_ui"
android:choiceMode="singleChoice"
android:scrollbars="vertical"
android:layout_weight="1"
android:textColor="@android:color/black" >
</ListView>
<ListView
android:id="@+id/to_list"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="@drawable/border_ui"
android:choiceMode="singleChoice"
android:scrollbars="vertical"
android:textColor="@android:color/black"
android:layout_weight="1"
/>
</LinearLayout>
列表片段列表未显示。我看到列表片段类 onCreateView 被调用。我怀疑自定义适配器需要修改。
列表片段使用适配器 as 并且列表不显示:
fromAdapter = new EntryArrayAdapter(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1, from);
toAdapter = new EntryArrayAdapter(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1, to);
自动完成视图使用它并显示其编辑文本字段但过滤不起作用:
fromList.setAdapter(new EntryArrayAdapter(getActivity().getApplicationContext(), android.R.layout.simple_dropdown_item_1line, from));
toList.setAdapter(new EntryArrayAdapter(getActivity().getApplicationContext(), android.R.layout.simple_dropdown_item_1line, to));
请指导。
问候,
米腾。