Udacity Sunshine app-lesson 1

时间:2016-07-16 06:14:15

标签: java android xml

我正在浏览udacity.com上的android dev培训,跟随sun app的实现。我正在使用android studio最新版本来实现。

我正准备到达我应该获得模拟列表视图的位置,而我在屏幕上什么都没有,并且没有显示任何错误。

这是我的mainActivity.java代码

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


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.fragment, 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.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);
    }

    /**
     * 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[] foreastarray={
                    "SUN-CLOUDY","MON-SUNNY","TUE-FOGGY","WED-CLOUDY","THU_ASTEROIDS","FRI-HEAVYRAIN","SAT-SUNNY"

            };
            List<String> weakforecast=new ArrayList<String>(Arrays.asList(foreastarray));
            ArrayAdapter<String> mForecastAdapter=new ArrayAdapter<String>(getActivity(),
                    R.layout.list_item_forecast,weakforecast);



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

            return rootView;
        }
    }
}

这是我的content_main.xml代码

     <fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment"
    android:name="com.example.android.sunshine.MainActivityFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:layout="@layout/fragment_main" />

这是我的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: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.sunshine.MainActivityFragment"
        tools:showIn="@layout/activity_main">

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

  </ListView>

  </FrameLayout>

最后这是我的list_item_forecast.xml代码

  <?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"
        android:gravity="center_vertical">
   </TextView>

请帮助我

2 个答案:

答案 0 :(得分:1)

您应该查看代码here。附件链接适用于相关分支。

content_main.xml中,你应该有一个FrameLayout,就像这样:

<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" />

通过这样做,一切都将落实到位。

您没有看到任何内容的原因是因为要将片段加载到容器中,该容器通常为FrameLayout。当您调用add(R.id.fragment, new PlaceholderFragment())时,实际上是将片段添加到第一个参数中传递了id的容器中,正如我之前所说的那样是一个FrameLayout。在您的情况下,您传递的是片段的ID,因此无法在其中进行渲染。

答案 1 :(得分:0)

我相信你的问题是你实际上在这里创建了2个片段,一个用XML声明,另一个(空)一个动态添加(不正确,正如Eric B.在他的回答中所指出的)在运行时隐藏了第一个片段。

您应该尝试删除活动中的交易&onCreate()。

以XML格式声明片段:https://developer.android.com/training/basics/fragments/creating.html

在运行时添加片段:https://developer.android.com/training/basics/fragments/fragment-ui.html