列表视图不会填充

时间:2015-09-22 03:32:18

标签: java android

我试图跟随Udacity的教程被卡在listview示例上以显示虚拟列表。

MainActivity.java:

package com.example.android.sunshine.app;

import android.app.Fragment;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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 java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @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);
    }

    public static class PlaceholderFragment extends Fragment {
        public PlaceholderFragment() {
        }


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

                // Create some dummy data for the ListView.  Here's a sample weekly forecast
                String[] data = {
                        "Mon 6/23 - Sunny - 31/17",
                        "Tue 6/24 - Foggy - 21/8",
                        "Wed 6/25 - Cloudy - 22/17",
                        "Thurs 6/26 - Rainy - 18/11",
                        "Fri 6/27 - Foggy - 21/10",
                        "Sat 6/28 - TRAPPED IN WEATHERSTATION - 23/18",
                        "Sun 6/29 - Sunny - 20/7"
                };
                List<String> weekForecast = new ArrayList<String>(Arrays.asList(data));

                // Now that we have some dummy forecast data, create an ArrayAdapter.
                // The ArrayAdapter will take data from a source (like our dummy forecast) and
                // use it to populate the ListView it's attached to.
                ArrayAdapter<String> forecastAdapter =
                        new ArrayAdapter<String>(
                                getActivity(), // The current context (this activity)
                                R.layout.list_item_forecast, // The name of the layout ID.
                                R.id.list_item_forecast_textview, // The ID of the textview to populate.
                                weekForecast);

                View rootView = inflater.inflate(R.layout.fragment_main, container, false);
                return rootView;
        }
    }
}

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=".MainActivityFragment">

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


</FrameLayout>

list_item_forecast.xml

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_item_forecast_textview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight" />

我几天都试图回溯教程。 请让我知道我哪里出错了。

谢谢。

3 个答案:

答案 0 :(得分:1)

在返回语句

之前添加这两行代码
ListView forecastlist=(ListView)findViewById(R.id.listview_forecast);
forecastlist.setAdapter(forecastAdapter);

答案 1 :(得分:0)

我的代码中没有看到ListView.setAdapter(ListAdapter adapter)来电。

ArrayAdapter<String> forecastAdapter =
                    new ArrayAdapter<String>(
                            getActivity(), // The current context (this activity)
                            R.layout.list_item_forecast, // The name of the layout ID.
                            R.id.list_item_forecast_textview, // The ID of the textview to populate.
                            weekForecast);

View rootView = inflater.inflate(R.layout.fragment_main, container, false);

((ListView) rootView.findViewById(R.id.listview_forecast)).setAdapter(forecastAdapter);

答案 2 :(得分:0)

我确实需要:

ListView forecastlist=(ListView)findViewById(R.id.listview_forecast);
forecastlist.setAdapter(forecastAdapter);

但我还需要将以下代码添加到:

protected void onCreate(Bundle savedInstanceState)方法。

 if (savedInstanceState == null)
        {
            getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
        }