Udacity- Android的第一课,列表视图显示不出来 [英] Udacity- Android First Lesson, Listview doesn't show up

查看:160
本文介绍了Udacity- Android的第一课,列表视图显示不出来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我检查了几次,从Udacity的视频,并试图做同样的事情,但列表视图显示不出来。能否请你检查我的code和告诉我,如果我错过了什么?

MainActivity.java

 公共类MainActivity扩展ActionBarActivity {    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        如果(savedInstanceState == NULL){
            getSupportFragmentManager()调用BeginTransaction()
                    。新增(R.id.container,新PlaceholderFragment())
                    。承诺();
        }
    }
    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.main,菜单);
        返回true;
    }    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        //处理动作栏项目点击这里。操作栏会
        //自动处理上点击主页/向上按钮,只要
        //你在AndroidManifest.xml中指定一个父活动。
        INT ID = item.getItemId();
        如果(ID == R.id.action_settings){
            返回true;
        }
        返回super.onOptionsItemSelected(项目);
    }    / **
     *包含一个简单视图中的占位符片段。
     * /
    公共静态类PlaceholderFragment扩展片段{        公共PlaceholderFragment(){
        }        @覆盖
        公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
                捆绑savedInstanceState){
            查看rootView = inflater.inflate(R.layout.fragment_main,集装箱,FALSE);           /*//我的方式
            ArrayList的<串GT; fakeData =新的ArrayList<串GT;();
            fakeData.add(今日恒业50/60);
            fakeData.add(明天,多云20/30);
            fakeData.add(星期三,雪40/50);
            fakeData.add(周四至多雨20/40); * /            //谷歌的方式
            的String [] = forecastArray {
                    今天,阳光50/60
                    明天,多云20/30
                    周三,雪40/50
                    周四多雨20/40
                    周五滑稽20/50
                    周六,阳光70/80
                    太阳阳光90/100
            };            ArrayList的<串GT; weekForecast =新的ArrayList<串GT;();
            Arrays.asList(weekForecast);            ArrayAdapter适配器=新ArrayAdapter<串GT;
                    (getActivity(),R.layout.list_item_forecast,R.id.list_item_forecast_textview,weekForecast);            ListView控件列表视图=(ListView控件)rootView.findViewById(R.id.listview_forecast);            listview.setAdapter(适配器);            返回rootView;
        }
    }
}

activity_main.xml中

 <的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:ID =@ + ID /容器
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    工具:上下文=。MainActivity
    工具:忽略=MergeRootFrame/>

fragment_main.xml

 <的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
    机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
    机器人:paddingTop =@扪/ activity_vertical_margin
    机器人:paddingBottom会=@扪/ activity_vertical_margin
    工具:上下文=$ MainActivity PlaceholderFragment。>    < ListView控件
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:ID =@ + ID / listview_forecast/>
< /&的FrameLayout GT;

list_item_forecast.xml

 < TextView中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    安卓了minHeight =机器人:ATTR /列表preferredItemHeight
    机器人:重力=center_vertical
    机器人:ID =@ + ID / list_item_forecast_textview>
< / TextView的>


解决方案

的问题是,weekForecast已宣告但尚未初始化。替换:

 的ArrayList<串GT; weekForecast =新的ArrayList<串GT;();
Arrays.asList(weekForecast);

 的ArrayList<串GT; weekForecast =新的ArrayList<串GT;(Arrays.asList(forecastArray));

I checked several times from Udacity videos and tried to do the exact same thing but listview doesn't show up. Could you please examine my code and tell me if I miss something?

MainActivity.java

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 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);

           /*//My way
            ArrayList<String> fakeData = new ArrayList<String>();
            fakeData.add("Today-Sunny 50/60");
            fakeData.add("Tomorrow-Cloudy 20/30");
            fakeData.add("Wednesday-Snowy 40/50");
            fakeData.add("Thursday-Rainy 20/40"); */

            //Google's way
            String[] forecastArray={
                    "Today-Sunny 50/60",
                    "Tomorrow-Cloudy 20/30",
                    "Wednesday-Snowy 40/50",
                    "Thursday-Rainy 20/40",
                    "Friday-Funny 20/50",
                    "Sat-Sunny 70/80",
                    "Sun-Sunny 90/100"
            };

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

            ArrayAdapter adapter = 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(adapter);

            return rootView;
        }


    }
}

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="MergeRootFrame"/>

fragment_main.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: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=".MainActivity$PlaceholderFragment">

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

list_item_forecast.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/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>

解决方案

The problem is that weekForecast has been declared but not initialized. Replace:

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

with:

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

这篇关于Udacity- Android的第一课,列表视图显示不出来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆