java - 绑定数据查找 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
抱歉,如果这些是基本问题,经过多年的挫折,我将放弃。
解决方案
第一:在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;
}
推荐阅读
- javascript - 从嵌套列表项类 javascript 中获取文本内容
- node.js - 在firebase云消息传递中发送带有图像的通知在nodeJS中不起作用
- apache-spark - 无法在 Spark 中安装 master
- node.js - 在 Firebase Cloud Functions 中安装私有 GitHub npm 包
- javascript - 在 id 相同的数组中添加值
- database - TDengine 数据库是否支持表/列名中的特殊字符?
- python - Pandas 中每个时间间隔包含多少条记录?
- python - 如何从数据框中计算事件的相对频率?
- vue.js - VueJS 3 + VueRouter 4 中没有路由名称
- python - 如何使用 LSTM 模型进行多步预测?