gpt4 book ai didi

Android:在 fragment 中显示列表

转载 作者:行者123 更新时间:2023-11-29 15:50:51 26 4
gpt4 key购买 nike

我有一个现有 fragment ,我想在其中显示典型地址类型的信息,例如街道、城市、PIN 和电话号码列表。

为了显示电话号码列表,我在 addressfragment.xml 中添加了 listview:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp" >

<TextView
android:id="@+id/txtStreetAddr"
style="?android:textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lineSpacingMultiplier="1.2"
android:text="default" />

<ListView
android:id="@+id/phones"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:paddingLeft="0dp" >
</ListView>
</LinearLayout>
</ScrollView>

我的 fragment 类:

        public class AddressFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

ViewGroup rootView = (ViewGroup) inflater
.inflate(R.layout.fragment_screen_slide_page, container, false);

((TextView) rootView.findViewById(R.id.txtStreetAddr)).setText(
"123, Corner Street");

ListView phoneListView = ((ListView) rootView.findViewById(R.id.phones));

ArrayList<String> phNumbers = new ArrayList<String>();
phNumbers.add("4343534343");
phNumbers.add("6767566766");

ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(MyApp.getAppContext(),android.R.layout.simple_list_item_1, phNumbers);
// Set The Adapter
phoneListView.setAdapter(arrayAdapter);
return rootView;
}

手机 ListView 没有显示在屏幕上 - 为什么?有更好的方法来处理这个问题吗?

最佳答案

我想你忘了在 onCreateView(...) 中膨胀 View

View rootView= inflater.inflate(R.layout.your_layout, container, false);

添加你的 onCreateView(...) 应该是

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

View rootView= inflater.inflate(R.layout.your_layout, container, false);

((TextView) rootView.findViewById(R.id.txtStreetAddr)).setText(
"123, Corner Street");

ListView phoneListView =((ListView) rootView.findViewById(R.id.phones);

ArrayList<String> phNumbers = new ArrayList<String>();
phNumbers.add("4343534343");
phNumbers.add("6767566766");

ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, phNumbers);
// Set The Adapter
phoneListView.setAdapter(arrayAdapter);

return rootView;
}

关于Android:在 fragment 中显示列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30118298/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com