我遇到问题是获取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的列表片段类被调用了。我怀疑自定义适配器需要修改。
list fragment使用适配器,而列表不显示:
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));
请指导。
此致
米滕。
答案 0 :(得分:0)
由于宽度和高度的设置,列表不会很好。我更改了列表布局,如下所示,显示正常。
<?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="match_parent"
android:orientation="horizontal" >
<ListView
android:id="@+id/from_list"
android:layout_width="wrap_content"
android:layout_height="match_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="match_parent"
android:background="@drawable/border_ui"
android:choiceMode="singleChoice"
android:scrollbars="vertical"
android:textColor="@android:color/black"
android:layout_weight="1"
/>
</LinearLayout>
为了使autoviewcomplete运行良好,我更改了客户适配器,如下所示: public static class FilteredEntryArrayAdapter extends ArrayAdapter&gt; { 列表&gt; data,filteredData; public FilteredEntryArrayAdapter(Context context,int textViewResourceId, 列表&gt;对象){ 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.setTextColor(Color.BLACK);
t.setText(filteredData.get(position).getValue());
t.setTag(filteredData.get(position).getKey());
Log.d(TAG, t.getText().toString());
return t;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
Log.d(TAG, "entryadapter:" + filteredData.size());
return filteredData.size();
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
Log.d(TAG, "getItemId called for " + arg0);
return filteredData.get(arg0).getKey().hashCode();
}
/**
* return CurrencyEntry whose toString is overridden to return only Value
*/
@Override
public SimpleImmutableEntry<String, String> getItem(int position) {
// TODO Auto-generated method stub
SimpleImmutableEntry<String,String> s = filteredData.get(position);
return new CurrencyEntry(s);
}
@Override
public Filter getFilter()
{
return new Filter()
{
@Override
protected FilterResults performFiltering(CharSequence charSequence)
{
FilterResults results = new FilterResults();
//If there's nothing to filter on, return the original data for your list
if(charSequence == null || charSequence.length() == 0)
{
results.values = data;
results.count = data.size();
}
else
{
ArrayList<AbstractMap.SimpleImmutableEntry<String,String>> filterResultsData = new ArrayList<AbstractMap.SimpleImmutableEntry<String,String>>();
for(AbstractMap.SimpleImmutableEntry<String,String> d : data)
{
//In this loop, you'll filter through originalData and compare each item to charSequence.
//If you find a match, add it to your new ArrayList
//I'm not sure how you're going to do comparison, so you'll need to fill out this conditional
if(d.getValue().toUpperCase().contains(charSequence.toString().toUpperCase()))
{
filterResultsData.add(d);
}
}
results.values = filterResultsData;
results.count = filterResultsData.size();
}
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults)
{
// Now we have to inform the adapter about the new list filtered
if (filterResults.count == 0)
notifyDataSetInvalidated();
else {
filteredData = (ArrayList<AbstractMap.SimpleImmutableEntry<String,String>>)filterResults.values;
notifyDataSetChanged();
}
}
};
}
}
对于在autocompletetext视图中显示良好的项目,我将列表条目更改为:
public static class CurrencyEntry扩展SimpleImmutableEntry {
public CurrencyEntry(Entry<? extends String, ? extends String> arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
public String toString() {
return getValue();
}
}
此致
米滕。