0

我在获取 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));

请指导。

问候,

米腾。

4

1 回答 1

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>

为了使自动查看完成功能正常工作,我将客户适配器更改如下: public static class FilteredEntryArrayAdapter extends ArrayAdapter> { List> data, filteredData; 公共FilteredEntryArrayAdapter(上下文上下文,int textViewResourceId,列表>对象){超级(上下文,textViewResourceId,对象);

        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();
                }
            }
        };
    }
}

为了在自动完成文本视图中很好地显示项目,我将列表条目更改为:

公共静态类 CurrencyEntry 扩展 SimpleImmutableEntry {

    public CurrencyEntry(Entry<? extends String, ? extends String> arg0) {
        super(arg0);
        // TODO Auto-generated constructor stub
    }

    public String toString() {
        return getValue();
    }
}

问候,

米腾。

于 2013-10-10T17:55:22.987 回答