简体   繁体   English

列表视图未填充

[英]List view does not populate

I am trying to following along the Udacity's tutorial got stuck on listview example to display the dummy list. 我正在尝试按照Udacity的教程使用listview示例显示虚拟列表。

MainActivity.java : 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: 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 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" />

I have been stuck on this a couple of days trying to retrace the tutorial. 我被困在这几天试图追溯教程。 Please let me know where I went wrong. 请让我知道我哪里出错了。

Thank you. 谢谢。

There are TWO problems in your code: 您的代码中有两个问题:

  1. You have not initialized the Listview using findViewById(...)
  2. You have not called the setAdapter on your listview object

solution: Add these two lines in your code before return statement 解决方案:在返回语句前的代码中添加这两行

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

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

EDIT(thanks to Lev) 编辑(感谢Lev)

public class MainActivity extends AppCompatActivity {

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

    getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}

... other code

Add this two lines of code before return statement 在return语句之前添加这两行代码

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

I don't see ListView.setAdapter(ListAdapter adapter) call in your code. 我在您的代码中看不到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);

I did need the: 我确实需要:

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

but I also needed to add the following code to the the : 但我还需要将以下代码添加到中:

protected void onCreate(Bundle savedInstanceState) method. protected void onCreate(Bundle savedInstanceState)方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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