gpt4 book ai didi

java - fragment 内的简单列表未填充

转载 作者:行者123 更新时间:2023-11-30 00:49:23 24 4
gpt4 key购买 nike

我在 Udacity 上开始了 Sunshine 项目。
我在第一章并试图填充 ListView 。但是我的 ListView 没有填充。在 Genymotion 模拟器上运行它,只显示一个带有操作栏的空白屏幕。

我的代码:
主 Activity .java

package snehit.sunshine.app;

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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}

public static class PlaceHolderFragment extends Fragment {
@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",
"tom - Sunny - 88/63",
"day after - Cyclone - 88/63",
"another day - Tornadoes - 88/63",
"One another day - Sunny - 88/63",
"Next day - Sunny - 88/63",
"again, next day - Sunny - 88/63"

};

List<String> weekForecast = new ArrayList<String>(
Arrays.asList(forecastArray)
);

ArrayAdapter mForecastAdapter = new ArrayAdapter(getActivity(),R.layout.list_item_forecast, R.id.list_item_forecast_textview,weekForecast);

ListView listview = (ListView) rootView.findViewById(R.id.listview_forecast);
listview.setAdapter(mForecastAdapter);
return rootView;
}
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
tools:ignore="MergeRootFrame"
android:id="@+id/container"
tools:context="snehit.sunshine.app.MainActivity">

</FrameLayout>

fragment _main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:paddingLeft="64dp"
android:paddingRight="64dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
tools:context=".MainActivity$PlaceHolderFragment"
android:layout_height="match_parent">

<ListView
android:layout_width="match_parent"
android:id="@+id/listview_forecast"
android:layout_height="match_parent" />
</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"
/>

我正在使用 Android Studio 2.1(我不知道使用 Eclipse 是否会有任何改变......我是 android 开发的新手)IDE 没有显示任何错误,应用程序启动时也没有崩溃。我在运行 Marshmallow (API 23) 的 Genymotion 模拟器上对其进行了测试

最佳答案

我想你忘了在 FrameLayout 中加载 fragment 请尝试下面的代码

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_stack_main"
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.mahii.notifybox.StackMainActivity">

<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</RelativeLayout>

Fragment class

public class StackFragment extends Fragment {

ListView listview_forecast;

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

View view = inflater.inflate(R.layout.fragment_stack, container, false);

listview_forecast = (ListView) view.findViewById(R.id.listview_forecast);

String forecastArray[] = {
"Today - Sunny - 88/63",
"tom - Sunny - 88/63",
"day after - Cyclone - 88/63",
"another day - Tornadoes - 88/63",
"One another day - Sunny - 88/63",
"Next day - Sunny - 88/63",
"again, next day - Sunny - 88/63"

};

List<String> weekForecast = new ArrayList<>(
Arrays.asList(forecastArray)
);

ArrayAdapter mForecastAdapter = new ArrayAdapter(getActivity(), R.layout.list_item_forecast, R.id.list_item_forecast_textview, weekForecast);
listview_forecast.setAdapter(mForecastAdapter);

return view;

}
}

fragment_stack.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

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

</LinearLayout>

最后在您的 MainActivity

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

StackFragment stackFragment = new StackFragment();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container, stackFragment, stackFragment.getClass().getName());
ft.commit();

}

这是输出

enter image description here

关于java - fragment 内的简单列表未填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41309940/

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