2016-12-24 4 views
1

Ich habe mit dem Projekt Sunshine auf Udacity begonnen.
Ich bin im ersten Kapitel und versuche, eine Listview zu füllen. Aber meine Listenansicht füllt nicht. Wenn Sie es auf dem Genymotion-Emulator ausführen, wird nur ein leerer Bildschirm mit der Aktionsleiste angezeigt.Einfache Liste innerhalb Fragment nicht bevölkern

Mein Code:
MainActivity.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

Ich benutze Android Studio 2.1 (Ich weiß nicht, ob die Verwendung von Eclipse ändern würde ... Ich bin neu in Android-Entwicklung) Die IDE zeigt keine Fehler, noch die App stürzt ab, wenn es ist gestartet. Ich habe es auf einem Genymotion-Emulator mit Marshmallow (API 23) getestet

Antwort

1

Ich glaube, Sie vergessen Fragment laden in FrameLayout Bitte versuchen Sie unter Code

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 Klasse

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> 

Und schließlich in Ihrem 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(); 

} 

Und hier ist der Ausgang

enter image description here

0

Gemäß dem Code, den Sie eingefügt haben, kann ich die Fragmenttransaktion in Ihrer Aktivität nicht sehen, was bedeutet, dass Sie kein Fragment in Ihre Aktivität aufgebläht haben. Es gibt 2 Möglichkeiten, um es

  1. Sie direkt Ihr PlaceHolderFragment im xml Ihrer acvity aufblasen kann.
  2. Sie können FragmentManager verwenden, um Fragment zu Ihrer Aktivitätsansicht hinzuzufügen.

Bitte beachten Sie den Link für weitere Informationen: Fragment in Activity

Danke und Grüße