Android ViewPager片段中的ListView未显示
这是片段的布局文件Android ViewPager片段中的ListView未显示,android,android-fragments,android-listview,android-viewpager,Android,Android Fragments,Android Listview,Android Viewpager,这是片段的布局文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/db1_root" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout androi
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/db1_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<ListView
android:id="@+id/channelMessageList"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<View
android:layout_height="1dip"
android:background="@android:color/black"
android:layout_width="fill_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|bottom"
android:orientation="horizontal"
android:paddingTop="2dip" >
<EditText
android:id="@+id/message_text"
android:layout_height="fill_parent"
android:layout_width="0dip"
android:layout_weight="1"
android:hint="@string/enter_message" />
<Button
android:id="@+id/send_button"
style="@style/button_text"
android:background="@drawable/send_message_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|right"
android:text="@string/send_message" />
</LinearLayout>
这是删除了不需要的位的代码
public class ChannelMessageListFragment extends Fragment {
ListView listView;
MessageMap _mInstance;
Button sendButton;
EditText messageView;
BroadcastReceiver updateReceiver;
List<String> messages;
ArrayAdapter<String> adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.activity_channel_message_list, container, false);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Bundle bundle = getArguments();
listView = (ListView) getView().findViewById(R.id.channelMessageList);
_mInstance = MessageMap.getInstance(getActivity());
String channelName = getResources().getString(R.string.app_name);
if(bundle != null) {
channelName = bundle.getString("channel_name");
}
getActivity().setTitle(channelName);
messages = _mInstance.getMessages(channelName);
if(messages == null) {
messages = new ArrayList<String>();
}
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, messages);
listView.setAdapter(adapter);
}
公共类ChannelMessageListFragment扩展了片段{
列表视图列表视图;
消息映射;
按钮发送按钮;
编辑文本信息视图;
广播接收器更新接收器;
列出信息;
阵列适配器;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图组根视图=(视图组)充气器。充气(
R.layout.activity\u channel\u message\u list,container,false);
返回rootView;
}
@凌驾
已创建ActivityState上的公共无效(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
Bundle=getArguments();
listView=(listView)getView().findViewById(R.id.channelMessageList);
_minInstance=MessageMap.getInstance(getActivity());
String channelName=getResources().getString(R.String.app_name);
if(bundle!=null){
channelName=bundle.getString(“channel_name”);
}
getActivity().setTitle(channelName);
messages=_mInstance.getMessages(channelName);
如果(消息==null){
messages=newarraylist();
}
adapter=newarrayadapter(getActivity(),android.R.layout.simple\u list\u item\u 1,messages);
setAdapter(适配器);
}
我调试了应用程序,消息列表不是空的,事实上,它包含了我需要显示的确切内容!如果有任何帮助,我们将不胜感激。能否更改您的:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<ListView
android:id="@+id/channelMessageList"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
到
我们的linearlayout(其中包含listview)有一个权重,但您的父布局没有权重和,请始终尝试设置视图的不同背景颜色,并查看是否出现。请记住,您向适配器提供了一个空数据集,listview将不会被填充。如前所述,我运行了调试模式并确认了数据aset不是空的你可以在这一行之前放一个日志,看看消息是否为空:if(messages==null){messages=new ArrayList();}我知道你说过消息不是空的,但我只是想确定它是否真的不是空的。你还可以更改你的:listView=(listView)getView().findViewById(R.id.channelMessageList);到listView=(listView)getActivity().findViewById(R.id.channelMessageList);我将尝试一下,就像更新一样:我制作了一个静态字符串数组,它显示出来了!关于列表是emtpy,我将添加日志并在我的机器上一拿到手就粘贴内容。
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="@+id/channelMessageList"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Log.i("Some Result", messages.toString());
if(messages == null) {
messages = new ArrayList<String>();
}
listView = (ListView) getView().findViewById(R.id.channelMessageList);
ListView listView = (ListView) getActivity().findViewById(R.id.channelMessageList);