2016-07-14 11 views
1

この質問は既に多くの時間尋ねられていますが、私はすべての回答に行きました。フラグメント内のリストビューには何も表示されません

したがって、アプリケーションの問題は、ArrayAdapterを使用してListListにArrayListを設定しようとしていることです。アプリケーションのMainActivityは、起動時に白い画面以外の何も表示しません。

は、ここでは、ListViewコントロールアダプタを設定すると 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> 
+0

あなたのページャアダプタのコードを共有しています。 –

+0

申し訳ありませんが、ページャアダプタはありません。 @ArpitRatan –

答えて

1

もちろん、アクティビティに何も表示されていない、あなたはあなたの活動の中にフラグメントを配置していません!でこれを使用してくださいあなたの活動のonCreate()R.id.containerがある

getSupportFragmentManager().beginTransaction() 
.replace(R.id.container, new PlaceholderFragment(), "fragment_tag").commit(); 

あなたmain_activity_layoutファイル内のレイアウト(空のレイアウト)

UPDATE:

activity_main:

は、これらの2ファイルを持っています

<?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); 
+0

私はこの方法を試してみましたが、今すぐアプリケーションが起動すると、アクティビティが2秒間表示されてから消えます。 (R.id。listview_forecast) btw、PlaceholderFragmentクラスはMainActivityクラスの内部にあります。 –

+0

2種類のレイアウトファイルを使用します。 1つはアクティビティ(コンテナになります)と1つはフラグメントのためのもので、その中にリストビューがあります。アクティビティレイアウトファイル内では、id = "container"のレイアウトを使用して、フラグメントで "fill"することができます –

+0

はい、私はそれをやっています。 fragment_main.xmlには、ListViewと1つのlist_item_forecast.xmlが含まれています。 私は残念ながら、他の人をそこに置くのを忘れてしまった。 –

0

だだ、アダプタの参照がnullで、 ListViewのnullアダプタを設定します。アダプタを設定する前にListViewアダプタを設定します。

ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast); 

    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 
    ); 
    listView.setAdapter(mForecastAdapter); 
+0

アダプタを定義した後にlistViewにmForeCastAdapterを設定しようとしましたが、問題は解決しません。 –

+0

FragmentManagerを使用して、MainActivityにフラグメントを追加する必要があります。 –

0

MainHctivity.javaファイル内にPlaceHolderFragmentはありますか?

もしそうなら、独自のファイルPlaceHolderFragment.javaでそれを分離してみてください。..

私の知る限りでは、Javaは、あなたの問題のことかもしれない理由..ソースファイルごとに1つのパブリッククラスができます。..

+0

これはまったく問題ではありません。これはjavaで許可されているためです。 –

+0

@GautamHans Lucaさんの答えはあなたの問題を解決します –

関連する問題