android列表视图设置适配器问题

时间:2016-09-24 16:54:03

标签: java android listview android-adapter

大家好,我是android的初学者。我使用udacity教程来倾斜它。在我的第一个项目中,我想构建一个天气应用程序。在开始我写了一些假数据,我想在屏幕上显示它。但我崩溃了。我不知道为什么

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(我使用它将数组放在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>

1 个答案:

答案 0 :(得分:0)

我很感谢这是一个问题的评论: textview必须是TextView而不是textView !!!!