Java 无法解析方法 setListAdapter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24076767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot resolve method setListAdapter
提问by Garf1eld
I try to show listView in fragment. But method setListAdapter - is not resolved. I think, that i must to get id of listView (android.R.id.list); and then : lv.setAdapter(mAdapter);but its dont work too.
我尝试在片段中显示 listView。但是方法 setListAdapter - 没有解决。我认为,我必须获得 listView (android.R.id.list) 的 id;然后: lv.setAdapter(mAdapter); 但它也不起作用。
public class MyEmployeeFragment extends Fragment {
private CustomAdapter sAdapter;
private List<User> userList;
ProgressDialog mProgressDialog;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
userList = new ArrayList<User>();
sAdapter = new CustomAdapter(getActivity(),userList);
setListAdapter(sAdapter);
new CustomAsync().execute();
}
private class CustomAdapter extends ArrayAdapter<User> {
private LayoutInflater inflater;
public CustomAdapter(Context context, List<User> objects) {
super(context, 0, objects);
inflater = LayoutInflater.from(context);
}
class ViewHolder {
TextView id;
TextView name;
TextView lastName;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vH;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item_employee, null);
vH = new ViewHolder();
vH.id = (TextView) convertView.findViewById(R.id.tv_employee_id);
vH.name = (TextView) convertView.findViewById(R.id.tv_employee_name);
vH.lastName = (TextView) convertView.findViewById(R.id.tv_employee_last_name);
convertView.setTag(vH);
} else
vH = (ViewHolder) convertView.getTag();
final User user = getItem(position);
vH.id.setText(user.getId());
vH.name.setText(user.getName());
vH.lastName.setText(user.getLastName());
return convertView;
}
}
private class CustomAsync extends AsyncTask<Void,Void,List<User>>
{}
}
XML
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:divider="#B7B7B7"
/>
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar2"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<TextView
android:id="@id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/empty"
android:layout_above="@+id/progressBar2"
android:layout_alignRight="@+id/progressBar2"
android:layout_alignEnd="@+id/progressBar2"
android:layout_marginBottom="83dp">
</TextView>
</RelativeLayout>
采纳答案by Raghunandan
setListAdapteris a method of ListFragmentwhile your fragment extends Fragment.
setListAdapter是一种ListFragmentwhile 你的片段 extends的方法Fragment。
So you either extend ListFragment
所以你要么扩展 ListFragment
or
或者
Extend Fragmentinflate a layout with ListView, initialize ListViewin onCreateViewof Fragmentand then use listView.setAdapter(adapter).
使用 扩展Fragment膨胀布局ListView,ListView在onCreateViewof 中初始化Fragment,然后使用listView.setAdapter(adapter)。
In case you want to extend Fragment
如果您想扩展 Fragment
<ListView
android:id="@+id/list"
Then
然后
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.yourxml, container, false);
ListView lv = (ListView)rootView.findViewById(R.id.list);
lv.setAdapter(youradapter);
return rootView;
}
回答by kalyan pvs
setListAdapter()is a method in ListFragment..For this you need to extend ListFragmnetinstead of Fragment.
setListAdapter()是ListFragment.. 中的一种方法,为此您需要扩展ListFragmnet而不是Fragment.
Change this line
改变这一行
public class MyEmployeeFragment extends Fragment
into
进入
public class MyEmployeeFragment extends ListFragment
回答by Eme
You need define the ListView first:
您需要先定义 ListView:
ListView list = (ListView) findById(android.R.id.list);
And later you need put the adapter:
稍后您需要放置适配器:
list.setAdapter(sAdapter);
Or change
或者换
extends Fragment
to
到
extends ListFragment

