Reputation: 53
I have started with the project Sunshine on Udacity.
I am on the very first chapter and trying to populate a listview. But my list view is not populating. Running it on the Genymotion emulator, only a blank screen is shown with the action bar.
My 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
<?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"
/>
I am using Android Studio 2.1(I don't know if using Eclipse would make any change...I am new to android development) The IDE does not show any errors, nor the app is crashing when it is started. I tested it on a Genymotion emulator running Marshmallow (API 23)
Upvotes: 1
Views: 86
Reputation: 4570
I think you forget to load fragment in FrameLayout
Please try below 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 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>
And finally in your 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();
}
And here is the output
Upvotes: 1
Reputation: 3112
As per the code you have pasted, I cannot see the Fragment Transaction in your activity that means you have not inflated fragment into your activity. There are 2 options to it
Please refer the link for more info: Fragment in Activity
Thanks and Regards
Upvotes: 0