2016-07-14 8 views
1

Ich weiß, dass diese Frage schon oft gestellt wurde, aber ich habe alle Antworten und keiner von ihnen für mich gearbeitet.ListView in Fragment zeigt nichts

Also, Problem mit der App ist, dass ich versuche, eine ArrayList in eine ListView mit einem ArrayAdapter zu füllen und die App MainActivity zeigt nichts als weiße Bildschirm beim Start.

Hier 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> 

Hier list_item_forecast.xml ist

<?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

Teilen Sie Ihre Pager-Adapter-Code. –

+0

Es tut mir leid, aber es gibt keinen Pager Adapter. @ArpitRatan –

Antwort

1

Natürlich zeigt die Aktivität nichts, Sie platzieren das Fragment nicht in Ihrer Aktivität! Verwenden Sie diese in Ihrer Tätigkeit der onCreate():

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

Wo R.id.container ist ein Layout in Ihrer main_activity_layout Datei (leer Layout)

UPDATE:

haben diese 2-Datei:

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> 

behalten Sie die 3 ° Layout-Datei, es ist in Ordnung.

nun in Ihrer Aktivität:

@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(); 
} 

auch:

 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

Ich habe diese Methode versucht und jetzt, wenn die App startet, kommt die Aktivität für eine Sekunde und verschwindet dann. (Loswerden.listview_forecast) Die Klasse PlaceholderFragment befindet sich in der MainActivity-Klasse. –

+0

Verwenden Sie 2 verschiedene Layoutdateien. Eine für die Aktivität (die ein Container sein wird) und eine für das Fragment, mit der Listenansicht darin. Verwenden Sie in der Aktivitätslayoutdatei ein Layout mit id = "container", damit Sie es mit dem Fragment "füllen" können. –

+0

Ja, das mache ich. fragment_main.xml enthält die ListView und eine list_item_forecast.xml. Es tut mir leid, dass ich vergessen habe, den anderen dort hinzustellen. –

0

Wenn Sie das Listview-Adapter-Set, der Verweis der Adapter ist null und Sie legen den Nulladapter für ListView fest. Sie sollten das erstellen Adapter vor dem ListView-Adapter.

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

Ich habe versucht, mForeCastAdapter zu listView nach der Definition des Adapters einzurichten, aber das Problem besteht fort. –

+0

Sie sollten den FragmentManager verwenden, um das Fragment in MainActivity hinzuzufügen. –

0

Ist Ihr PlaceHolderFragment in Ihrer MainActivity.java-Datei?

Wenn ja, versuchen Sie es auf einer eigenen Datei PlaceHolderFragment.java Trennung ..

Soweit ich weiß, Java nur eine öffentliche Klasse pro Quelldatei erlaubt .. Das vielleicht der Grund des Problems ..

+0

sollte das überhaupt kein Problem sein. weil es in Java erlaubt ist. –

+0

@GautamHans Lucas Antwort wird Ihr Problem lösen –