ArrayAdapter with ArrayList

  • Replies:1
  • OpenNot stickiedUnanswered
  • Forum posts: 1

Sep 7, 2015 2:01:33 PM via Website

Hello,
I am trying to populate an arrayList using arrayAdapter as ListView into a custom Layout in Android Studio, everything is fine not giving any error! but when i am running the app it loads okay but show nothing!
bellow are my codes:

MainActicity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_item_forecast);
}


@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_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);



}


public static class PlaceHolderFragment extends Fragment{
    private ArrayAdapter<String> mForecastAdapter;
    public  PlaceHolderFragment(){

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View rootView = inflater.inflate(R.layout.fragment_main, container);

        String[] forecastArray = {
                "Today - Sunny - 88/63",
                "Tomorrow - Foggy - 88/63",
                "Weds - Sunny - 88/63",
                "Thurs - Sunny - 88/63",
                "Fri - Sunny - 88/63",
                "Sat - Sunny - 88/63",
                "Sun - Sunny - 88/63"

        };


        List<String> weekForecast  = new ArrayList<String>(
                Arrays.asList(forecastArray));

        mForecastAdapter = new ArrayAdapter<String>(

                getActivity(),
                R.layout.list_item_forecast, R.id.list_item_forecast_textview, weekForecast);

        ListView lv = (ListView) rootView.findViewById(R.id.listview_forecast);
        lv.setAdapter(mForecastAdapter);

        return rootView;
    }
}

}

list_item_forecast.xml

<TextView
    xmlns: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>

fragment_main.xml

<FrameLayout xmlns:android="..."
    xmlns:tools=".."
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivityFragment">

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

</FrameLayout>

Thanks so much in advance

  • Forum posts: 207

Sep 10, 2015 2:30:48 PM via Website

PlaceHolderFragment where you are calling this fragment which has the list adapter????? Call the PlaceHolderFragment you list will populate.