【问题标题】:Listview item not Display in the Fragment AndroidListview 项目未在 Fragment Android 中显示
【发布时间】:2020-04-06 22:27:30
【问题描述】:

我有带有片段的导航抽屉。我想在导航抽屉上打开片段。代码中没有任何错误,但 Listview 项目未显示在片段中。仅显示空白页。遵循代码。请帮帮我。

fragment_blank_fragment_trip2.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.administrator.vtsapplication.BlankFragmentTrip2">

<ListView
android:id="@+id/mobile_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

Activity_listview.xml

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >

BlankFragmenttrip2.java

package com.example.administrator.vtsapplication;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class BlankFragmentTrip2 extends Fragment {
public BlankFragmentTrip2() {
    // Required empty public constructor
}
ViewGroup vg;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    String[] mobileArray = {"Android", "IPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X"};
    vg = (ViewGroup) inflater.inflate(R.layout.fragment_blank_fragment_trip2, container, false);
    ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(), R.layout.activity_listview, mobileArray);

    ListView listView = (ListView) vg.findViewById(R.id.mobile_list);
    listView.setAdapter(adapter);
    return inflater.inflate(R.layout.fragment_blank_fragment_trip2, container, false);
}

}

【问题讨论】:

  • 删除最后一行,我认为这是返回一个空视图。只返回vg,不要发送ViewGroup,因为方法onCreateView()的返回类型只有View

标签: android xml listview layout fragment


【解决方案1】:

您应该像这样使用您的Fragment。你没有返回你的View

public class BlankFragmentTrip2 extends Fragment {

    String[] mobileArray = {"Android", "IPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X"};

    public BlankFragmentTrip2() {
        // Required empty public constructor
    }


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

        View rootView = inflater.inflate(R.layout.fragment_blank_fragment_trip2, container, false);
        ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(), R.layout.activity_listview, mobileArray);

        ListView listView = (ListView) rootView.findViewById(R.id.mobile_list);
        listView.setAdapter(adapter);
        return rootView;
    }

}

【讨论】:

  • 感谢您的回复。使用您的代码解决了它的工作和我的问题。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 2021-03-03
  • 2017-05-09
相关资源
最近更新 更多