片段中的ListView没有显示任何内容

时间:2016-07-14 14:26:25

标签: java android listview android-fragments fragment

我知道这个问题已经被问过了很多次但是我已经找到了所有答案,但没有一个能为我工作。

因此,应用程序的问题在于我尝试使用ArrayAdapter将ArrayList填充到ListView中,并且应用程序MainActivity在启动时不会显示任何白色屏幕。

下面' S MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}


public static class PlaceholderFragment extends Fragment{

    private ArrayAdapter<String> mForecastAdapter;

    public PlaceholderFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        //Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        String[] forecastArray = {
                "Today - Sunny - 88/63",
                "Tomorrow - Foggy - 70/46",
                "Weds - Cloudy - 72/63",
                "Thurs - Rainy - 64/51",
                "Fri - Foggy - 70/46",
                "Sat - Sunny - 76/68"
        };

        List<String> weekForecast = new ArrayList<String>(Arrays.asList(forecastArray));
        ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
        listView.setAdapter(mForecastAdapter);

        mForecastAdapter = new ArrayAdapter<String>(
                //The current context (current activity)
                getActivity(),
                //ID of list item layout
                R.layout.list_item_forecast,
                //id of the textView to populate
                R.id.list_item_forecast_textview,
                //data to populate
                weekForecast
        );

        return rootView;
    }
} 

fragment_main.xml

<FrameLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.gautamhans.sunshine.app.MainActivityFragment"
    tools:showIn="@layout/activity_main">

    <ListView
        android:id="@+id/listview_forecast"
        android:layout_width="match_parent"
        android:layout_height="667dp"
        />

</FrameLayout>

这里是list_item_forecast.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:id="@+id/list_item_forecast_textview"
    >

</TextView>

3 个答案:

答案 0 :(得分:1)

当然活动没有显示任何内容,您不会将片段放在活动中!在您的活动onCreate()

中使用此功能
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new PlaceholderFragment(), "fragment_tag").commit();

其中R.id.container是main_activity_layout文件中的布局(空布局)

更新:

有这两个文件:

activity_main:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

</RelativeLayout>

fragment_main:

<FrameLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.gautamhans.sunshine.app.MainActivityFragment"
    tools:showIn="@layout/activity_main">

    <ListView
        android:id="@+id/listview_forecast"
        android:layout_width="match_parent"
        android:layout_height="667dp"
        />

</FrameLayout>

保留3°布局文件,没关系。

现在,在您的活动中:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getSupportFragmentManager().beginTransaction()
    .replace(R.id.container, new PlaceholderFragment(), "fragment_tag").commit();
}

也:

        mForecastAdapter = new ArrayAdapter<String>(
                //The current context (current activity)
                getActivity(),
                //ID of list item layout
                R.layout.list_item_forecast,
                //id of the textView to populate
                R.id.list_item_forecast_textview,
                //data to populate
                weekForecast
        );
//move this after the initialization of adapter
listView.setAdapter(mForecastAdapter);

答案 1 :(得分:0)

设置ListView适配器时,适配器的引用为null,并为ListView设置null适配器。您应该在设置ListView适配器之前创建适配器。

R.utils::evalWithTimeout({
    repeat {
      res <- try(
        # Your code
      })
      # Handle error from try if necessary:
      if (class(res) != "try-error") {
        # Your code
      }
    }
  }, timeout = 15*60, onTimeout = "warning")

答案 2 :(得分:0)

您的PlaceHolderFragment是否在MainActivity.java文件中?

如果是这样,请尝试将其分隔在自己的文件PlaceHolderFragment.java ..

据我所知,java每个源文件只允许一个公共类..这可能是你问题的原因..