我创建了一个TabbedActivity作为我的APP的主屏幕,我也创建了一个片段(在TabbedActivity中使用),以便在点击特定标签时显示。 在我正在使用AutoComplteTextView的片段之一 问题是AutoComplteTextView没有显示字符串数组列表中提到的任何建议,我也没有在日志中收到任何错误 可以直接解决这个问题 码 从中调用片段的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;
}
}
Fragment(fragment_make_transaction)具有AutocompletTextView
的代码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和导航样式为-ActionBarTabs(带页面查看器),如下图所示TabbedActivity
编辑片段中的代码,如下所示 片段代码截至目前
package demo.com.problem;
import android.content.Context; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;
公共类BlankFragment扩展Fragment { @覆盖 public View onCreateView(LayoutInflater inflater,ViewGroup容器, Bundle savedInstanceState){ //为此片段扩充布局 return inflater.inflate(R.layout.fragment_blank,container,false); } }
转到TabbedActivity - 方法内的java代码(onCreateView) 使用以下代码调用片段
当用户在选项卡式活动的第一个屏幕上时,这将调用片段
@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添加到片段
的步骤片段的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" />
片段的Java代码
package demo.com.problem;
import android.content.Context; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v7.widget.AppCompatAutoCompleteTextView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView;
公共类BlankFragment扩展Fragment { AutoCompleteTextView autoCompleteTextView; private static final String [] COUNTRIES = new String [] {&#34; India&#34;,&#34; Aus&#34;};
@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;
}
}
自动填充文字视图enter image description here
的问题