Jeetesh Saxena
Jeetesh Saxena

Reputation: 11

List view not getting populated by array adapter

I am new to android and have been following udacity's video lectures. The list view is not getting populated by the array adapter and when i ran the app on mobile the app showed only the main activity layout.

the list view is not appearing on the mobile screen ,can anyone tell me whats the error here,is it something related to frame layout or is there an error in array adapter. Thanks in advance

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","Tommorow","wed","Thursday","Friday","Saturday","Sunday"
        };//creating some fake data to show in list view
        List<String> fore=new ArrayList<String>(Arrays.asList(forecastArray));
        mForecastAdapter=new ArrayAdapter<String>(getActivity(),R.layout.list_item_forecast,R.id.list_item_forecast_textview,fore);
        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:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container"
tools:context="com.saxenarc.jeetesh.layouts.MainActivity">
</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:id="@+id/list_item_forecast_textview"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
>

</TextView>

fragment_main.xml

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

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

</FrameLayout>

Checked everything thrice before posting it.

Upvotes: 0

Views: 101

Answers (1)

Lucem
Lucem

Reputation: 3082

Your code and list is working well but you haven't told the list where to be displayed. You can fix this in 2 ways.


First

Add the fragment inside the main activity since its not visible.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
        fragmentManager.beginTransaction().replace(R.id.container, new PlaceHolderFragment).commit();
    }

Second

Stop using the fragment and use the listview inside the Main activity

activity_main.xml

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

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

</FrameLayout>

MainActivity.java

List<String> fore = new ArrayList<String>(Arrays.asList(forecastArray));
mForecastAdapter = new ArrayAdapter<String>(this,R.layout.list_item_forecast,R.id.list_item_forecast_textview,fore);
ListView listview= (ListView) findViewById(R.id.listview_forecast);
listview.setAdapter(mForecastAdapter);

Upvotes: 2

Related Questions