问题描述
我尝试显示listView的片段.但是方法setListAdapter-尚未解决.我认为,我必须获取listView(android.R.id.list)的ID;然后:lv.setAdapter(mAdapter);但是它也不起作用.
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.
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 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>
推荐答案
setListAdapter
是 ListFragment
的一种方法,而您的片段扩展了 Fragment
.
setListAdapter
is a method of ListFragment
while your fragment extends Fragment
.
所以您可以扩展 ListFragment
或
使用 ListView
扩展 Fragment
填充布局,在 Fragment 的
onCreateView
中初始化 ListView
代码>,然后使用 listView.setAdapter(adapter)
.
Extend Fragment
inflate a layout with ListView
, initialize ListView
in onCreateView
of Fragment
and then use listView.setAdapter(adapter)
.
如果您想扩展Fragment
In case you want to extend Fragment
<ListView
android:id="@+id/list"
然后
@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;
}
这篇关于无法解析方法setListAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!