0

I am trying to populate fake data using Fragments/ListView/Arrays & Adapter. but when i run the App , i am getting blank screen, Please guide::

below is my MainActivity.Java codes within Fragment class::

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

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


    String[] forecastArray = {
            "Today , Sunny , 88/63",
            "Tomorrow , Sunny , 88/63",
            "Wesneday , Sunny , 88/63",
            "THursday , Sunny , 88/63",
            "FRIDAY , Sunny , 88/63",
            "Saturday , Sunny , 88/63",
            "Sunday , Sunny , 88/63",
    };

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

        ArrayAdapter<String> mForecastAdapter;
        mForecastAdapter = new ArrayAdapter<String>(
                    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;

}
ayazk
  • 43
  • 1
  • 5
  • can you post the List_item_forecast_textView xml please ? In order to make Array adapter to use your custom layout for list item, your custom layout must conatin just one TextView with id android:id="@android:id/text1". Please check if your List_item_forecast_textView has one TextView with id @android:id/text1 ?? – Sandeep Bhandari Apr 07 '16 at 09:25
  • @SandeepBhandari FYI Please – ayazk Apr 07 '16 at 10:12
  • As I mentioned earlier can you please change the TextView id to android:id="@android:id/text1"?? and try running it again ?? – Sandeep Bhandari Apr 07 '16 at 10:20
  • @ayazak : I know you have a id my friend but array adapter wont understand that id correct ?? It always looks for id called text1 :) So please change your TextView id to android:id="@android:id/text1" :) – Sandeep Bhandari Apr 07 '16 at 10:24
  • Any luck??? ayazk :) – Sandeep Bhandari Apr 07 '16 at 10:34
  • @SandeepBhandari what to be changed from mainActivity.Java side accordingly? – ayazk Apr 07 '16 at 10:35
  • Shouldn't need any change in mainActivity :| what happenned still not working ??? – Sandeep Bhandari Apr 07 '16 at 10:36
  • i have changes ID as you mentioned in XML layout but this time it is giving error in MainActivity.Java – ayazk Apr 07 '16 at 10:37
  • What error buddy ??? :) – Sandeep Bhandari Apr 07 '16 at 10:37
  • ** R.id.List_item_forecast_textView, weekForecast);** – ayazk Apr 07 '16 at 10:40
  • Cannot resolve symbol ' List_item_forecase_textView' – ayazk Apr 07 '16 at 10:41
  • OK replace R.id.List_item_forecast_textView with android.R.id.text1 :) Please clean and make build and check :) and one more check before creating Array adapter please check weekForecast list is not empty :) Put a break point check and leme know :) – Sandeep Bhandari Apr 07 '16 at 10:43
  • @SandeepBhandari do you have any reference for the need of 'text1' id? It doesn't matter what you name it. It should be TextView is all. – Raghubansh Mani Apr 07 '16 at 10:49
  • @RaghubanshMani : Yes. Here is a link http://stackoverflow.com/questions/28192815/how-to-add-customised-layout-to-arrayadapter. You can check ρяσѕρєя K answer as well as I have tried it myself :) And it requires it to be text1 :) – Sandeep Bhandari Apr 07 '16 at 10:52
  • @SandeepBhandari I am sorry brother, but it seems incorrect to me. Here is the code for ArrayAdapter https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/ArrayAdapter.java. Look at the constructors and then look at the getView method. Check mFieldId and what it means to be 0. When you supply a layout resource, then you should also supply a TextView resource id in that layout, otherwise it assumes that the whole layout is just a TextView. No mention of any 'text1' anywhere. It sounds weird that android would want you to give a particular name to something. – Raghubansh Mani Apr 07 '16 at 11:07
  • @raghubansh-mani May be you are right :) Am not an expert either :) As I know I have used ArrayAdapter( this,my_layout_id,items) and kept my custom cell to be just a TextView with some diff id It din work :) After browsing a bit I realised on changing it to android.R.id.text1 it works :) I tried and it worked :) Thats why I was asking him to give a try rather then telling this is the answer :) I'll keep in mind your point :) – Sandeep Bhandari Apr 07 '16 at 11:12
  • @ayazk Nothing seems to be wrong with the code you have shown us. Have you added this fragment to the MainActivity? – Raghubansh Mani Apr 07 '16 at 11:19
  • @RaghubanshMani Yes, this frament code is part of MainActivity, – ayazk Apr 07 '16 at 19:33
  • @RaghubanshMani Dude it did'nt work yet, still showing blank screen :( – ayazk Apr 07 '16 at 19:34
  • @ayazk Sorry but I can't find anything wrong with the code you have attached here. Need to see more code, MainActivity.java, related xml files, etc. Why don't you try a few things to check what is not coming up, say, give random background color to all your layouts everywhere and see which shows up and which doesn't. – Raghubansh Mani Apr 08 '16 at 06:32
  • How to post main activity.java because there is size limitation here in comment – ayazk Apr 08 '16 at 06:47
  • @ayazk Did you try my suggestion of background colors? – Raghubansh Mani Apr 11 '16 at 09:50
  • @RaghubanshMani Sorry to not update you, no i didn't do that instead i wrote the code again from scratch and it worked this time :) – ayazk Apr 12 '16 at 09:33

1 Answers1

0

Maybe it's because you are populating your adapter in onCreateView. Try to do it on onResume.

Keep a reference to rootView and do this part on onResume

ArrayAdapter<String> mForecastAdapter;
    mForecastAdapter = new ArrayAdapter<String>(
                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);
loukaspd
  • 90
  • 7