我正在尝试在android studio中使用列表视图。
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
populateListView();
return inflater.inflate(R.layout.stand1,container,false);
}
private void populateListView() {
String[] pair = {"Pair1","Pair2","Pair3","Pair4","Pair5"};
//build adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.stand1,pair);
//populate
ListView list = (ListView) findViewById(R.id.Stand1list);
list.setAdapter(adapter);
}}
我得到的错误是
Error:(31, 40) error: no suitable constructor found for ArrayAdapter(Stand1,int,String[])
constructor ArrayAdapter.ArrayAdapter(Context,int,int) is not applicable
(argument mismatch; Stand1 cannot be converted to Context)
constructor ArrayAdapter.ArrayAdapter(Context,int,String[]) is not applicable
(argument mismatch; Stand1 cannot be converted to Context)
constructor ArrayAdapter.ArrayAdapter(Context,int,List<String>) is not applicable
(argument mismatch; Stand1 cannot be converted to Context)
Error:(34, 36) error: cannot find symbol method findViewById(int)
stand1是一个xml文件,Stand1List是stand1.xml
中的listview id我知道这很简单,但对于我的生活,我不知道是什么......
对于引用,Stand1.java是java文件,它是activity_main中的一个片段。它被导航抽屉调用。
更新
感谢Jedil答案,我有点工作。但是当我加载片段说文本视图需要ID时,我遇到了错误。 我没有在stand1.xml下面放一个textview。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/Stand1list">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tex1"
/>
</ListView>
</FrameLayout>
现在我收到错误
java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
答案 0 :(得分:0)
您过早地调用populateListView()
,因为您的视图尚未创建。
尝试使用
populateListView()
public void onViewCreated (View view, Bundle savedInstanceState)
答案 1 :(得分:0)
将ListView声明为fragment
:
ListView list; // your ListView
而不是将其传递给您的ArrayAdapter
,您需要传递getActivity()
,这是主持人活动的背景。
最后,在onCreateView
方法中初始化listView。
list = (ListView) findViewById(R.id.Stand1list);
<强> CODE:强>
ListView list; // your ListView
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.stand1,container,false);
list = (ListView) view.findViewById(R.id.Stand1list);
populateListView();
}
private void populateListView(View mView) {
String[] pair = {"Pair1","Pair2","Pair3","Pair4","Pair5"};
//build adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.stand1,pair);
list.setAdapter(adapter);
}
答案 2 :(得分:0)
您的代码是fragment
实施代码。
第一个错误是关于ArrayAdapter
需要Context
作为第一个参数。
因此,代码位置this
表示fragment
个对象,但您需要Context
Activity
,而不是使用this
使用getActivity()
在那些地方
ArrayAdapter<String>(getActivity(), R.layout.stand1,pair);
和
ListView list = (ListView) getActivity().findViewById(R.id.Stand1list);
那么代码应该如何:
ListView mList;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.stand1,container,false);
mList = (ListView) root.findViewById(R.id.Stand1list);
return root;
}
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
populateListView();
}
private void populateListView() {
String[] pair = {"Pair1","Pair2","Pair3","Pair4","Pair5"};
//build adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.stand1,pair);
mList.setAdapter(adapter);
}}
答案 3 :(得分:0)
将代码更改为:
View view = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.stand1,container,false);
populateListView(view);
return view;
}
private void populateListView(View mView) {
String[] pair = {"Pair1","Pair2","Pair3","Pair4","Pair5"};
//build adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.stand1,pair);
//populate
ListView list = (ListView) mView.findViewById(R.id.Stand1list);
list.setAdapter(adapter);
}}
答案 4 :(得分:0)
ListView listview; // define this on class level
View view = inflater.inflate(R.layout.stand1,container,false);
listview = view.findViewById(R.id.Stand1list);
populateListView();
retur view ;
在populateListView()
中以这种方式设置适配器
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.stand1,pair);