简体   繁体   中英

android list view set adapter issue

hi guys i am a beginner in android. and i use udacity tutorials to lean it. in my first project i want to build a weather app. in the beginning i write some fake data and i want to show it on screen . but i crash . i dont know why

this is mainActivityfragment.java
public class MainActivityFragment extends Fragment {
    private ArrayAdapter<String> mForecastAdapter;
    public MainActivityFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View screen = inflater.inflate(R.layout.fragment_main, container, true);
        String[] forecastArray ={

                "today - sunny - 88/63",
                "today - sunny - 88/63",
                "weds - cloudy - 72/63",
                "thurs - asteroids - 75/65",
                "fri - heavy rain - 65/56",
                "sat - sunny - 60/51"
        };

        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 mylistv =(ListView) screen.findViewById(R.id.listview_forecast);
       mylistv.setAdapter(mForecastAdapter);
        return screen;

    }

main_fragment.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.example.android.badsaba.MainActivityFragment"
    tools:showIn="@layout/activity_main">

    <ListView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:id = "@+id/listview_forecast"
         />

</FrameLayout>

list_item_forecast.xml (i use it to put array on its textview)

<?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="match_parent"
    android:id = "@+id/list_item_forecast_textview"
    >

</textview>

我很欣赏这个问题:textview必须是TextView而不是textView !!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM