首页 > 解决方案 > 绑定数据查找 ListView 的问题

问题描述

我正在尝试了解 Java ListView 组件,并且我想在加载片段时在其上添加数据,所以我在onCreateView方法中执行此操作。如果我错了,请纠正我:

Public class JobsListFragment  extends Fragment {
ListView listView ;
private ArrayAdapter<String> listAdapter ;

@Nullable
@Override
 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_jobs_list, container, false);

    // Find the ListView resource.
      View rootView = inflater.inflate(R.layout.fragment_jobs_list, container, false);

    // Create and populate a List of planet names.
    String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",
            "Jupiter", "Saturn", "Uranus", "Neptune"};

    ArrayList<String> planetList = new ArrayList<String>();
    planetList.addAll( Arrays.asList(planets) );

    listAdapter = new ArrayAdapter<String>(this, R.layout.joblayoutrow, planetList);

    listAdapter.add( "Ceres" );
    listAdapter.add( "Pluto" );
    listAdapter.add( "Haumea" );
    listAdapter.add( "Makemake" );
    listAdapter.add( "Eris" );

    // Set the ArrayAdapter as the ListView's adapter.
    mainListView.setAdapter( listAdapter );
}

我遇到的问题是这两行:

    mainListView = (ListView) findViewById( R.id.mainListView );

    listAdapter = new ArrayAdapter<String>(this, R.layout.joblayoutrow, planetList);

所以我尝试使用 inflate ,我也找不到 ListView 。这是我的 ListView 片段:

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".JobsListFragment">


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

另外,因为我在这里使用片段,我似乎无法访问setContentView. Xamarin抱歉,如果这些是基本问题,经过多年的挫折,我将放弃。

标签: javaandroid

解决方案


第一:在return语句之后,什么都不会执行

第二:使用 rootVew 来初始化里面,R.layout.fragment_jobs_list因为R.layout.fragment_jobs_list布局里面的任何东西,现在都在里面rootView

第三:返回rootVIew

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

    // Find the ListView resource.
      View rootView = inflater.inflate(R.layout.fragment_jobs_list, container, false);
     mainListView = (ListView) rootView.findViewById( R.id.mainListView );


    // Create and populate a List of planet names.
    String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",
            "Jupiter", "Saturn", "Uranus", "Neptune"};

    ArrayList<String> planetList = new ArrayList<String>();
    planetList.addAll( Arrays.asList(planets) );

    listAdapter = new ArrayAdapter<String>(this, R.layout.joblayoutrow, planetList);

    listAdapter.add( "Ceres" );
    listAdapter.add( "Pluto" );
    listAdapter.add( "Haumea" );
    listAdapter.add( "Makemake" );
    listAdapter.add( "Eris" );

    // Set the ArrayAdapter as the ListView's adapter.
    mainListView.setAdapter( listAdapter );
    return rootView;

}

推荐阅读