0

我创建了一个 TabbedActivity 作为我的 APP 的主屏幕,我还创建了一个片段(在 TabbedActivity 内部使用)以在单击特定选项卡时显示。在我正在使用 AutoComplteTextView 的片段之一中,问题是 AutoComplteTextView 不显示字符串数组列表中提到的任何建议,而且我在日志中也没有收到任何错误,可以直接解决这个问题 Code TabbedActivity 从哪里调用片段

  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            if (getArguments().getInt(ARG_SECTION_NUMBER) == 1){
                View viewMakeTransaction = inflater.inflate(R.layout.fragment_make_transaction, container, false);
                return viewMakeTransaction;
            }
            else if(getArguments().getInt(ARG_SECTION_NUMBER) == 2){
                View viewAddCustomer = inflater.inflate(R.layout.fragment_add_customer, container, false);
                return viewAddCustomer;
            }
            else if (getArguments().getInt(ARG_SECTION_NUMBER) == 3){
                View viewViewTransaction = inflater.inflate(R.layout.fragment_view_transaction, container, false);
                return viewViewTransaction;
            }
            else{

                View rootView = inflater.inflate(R.layout.fragment_main, container, false);
                TextView textView = (TextView) rootView.findViewById(R.id.section_label);
                textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
                return rootView;
            }
        }

具有 AutocompletTextView 的片段 (fragment_make_transaction) 代码

private static final String[] COUNTRIES = new String[]{"India","America"};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View viewMakeTransactionFragment = inflater.inflate(R.layout.fragment_make_transaction, container, false);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,COUNTRIES);
    final AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) viewMakeTransactionFragment.findViewById(R.id.selectCustomer);
    autoCompleteTextView.setAdapter(adapter);
    return viewMakeTransactionFragment;
}

重现问题的步骤 1. 创建一个新的 Android 项目 - 选择 TabbedActivity 和 Navigation Style 作为 -ActionBarTabs(with page viewer) 如下图TabbedActivity

  1. 单击完成 - 将创建项目
  2. 创建一个新的空白片段
  3. 从现在开始编辑片段中的代码,如下片段代码

    包 demo.com.problem;

    导入android.content.Context;导入android.net.Uri;导入android.os.Bundle;导入android.support.v4.app.Fragment;导入 android.view.LayoutInflater;导入android.view.View;导入android.view.ViewGroup;

    public class BlankFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // 膨胀这个片段的布局 return inflater.inflate(R.layout.fragment_blank, container, false); } }

  4. 转到 TabbedActivity - 方法 (onCreateView) 内的 java 代码使用以下代码调用片段

当用户在选项卡式活动的第一个屏幕上时,这将调用片段

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    if (getArguments().getInt(ARG_SECTION_NUMBER) == 1){
        View viewMakeTransaction = inflater.inflate(R.layout.fragment_blank, container, false);
        return viewMakeTransaction;
    }
    else{

        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        TextView textView = (TextView) rootView.findViewById(R.id.section_label);
        textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
        return rootView;
    }
}

运行应用程序一次以确保片段显示在第 1 部分下

将 AutoCompleteTextView 添加到片段的步骤

  1. 片段的 XML 文件

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_blank_fragment" />
    
    <AutoCompleteTextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    

  2. 片段的 Java 代码

    包 demo.com.problem;

    导入android.content.Context;导入android.net.Uri;导入android.os.Bundle;导入android.support.v4.app.Fragment;导入 android.support.v7.widget.AppCompatAutoCompleteTextView;导入 android.view.LayoutInflater;导入android.view.View;导入android.view.ViewGroup;导入 android.widget.ArrayAdapter;导入 android.widget.AutoCompleteTextView;

    公共类 BlankFragment 扩展片段 { AutoCompleteTextView autoCompleteTextView; private static final String[] COUNTRIES = new String[]{"India","Aus"};

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    
        View view = inflater.inflate(R.layout.fragment_blank, container, false);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,COUNTRIES);
        autoCompleteTextView = (AutoCompleteTextView) view.findViewById(R.id.textView);
        autoCompleteTextView.setAdapter(adapter);
        // Inflate the layout for this fragment
        return view;
    }
    

    }

自动完成文本视图的问题在此处输入图像描述

4

0 回答 0