ViewPager片段中的ListView未显示

时间:2014-03-16 11:47:25

标签: 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
    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);

}

我调试了应用程序并且消息列表不为空,实际上它具有我需要显示的确切内容!任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

你可以改变你的:

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

我知道你说消息不是null但我只是想确定它是不是真的。

而且, 尝试更改:

listView = (ListView) getView().findViewById(R.id.channelMessageList); 

 ListView listView = (ListView) getActivity().findViewById(R.id.channelMessageList);