gpt4 book ai didi

java - 无法解析方法 setListAdapter

转载 作者:行者123 更新时间:2023-11-29 10:13:52 25 4
gpt4 key购买 nike

我尝试在 fragment 中显示 listView。但是方法 setListAdapter - 没有解决。我认为,我必须获取 listView 的 id (android.R.id.list);然后: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 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>

最佳答案

setListAdapterListFragment 的一种方法当你的 fragment 延伸 Fragment .

http://developer.android.com/reference/android/app/ListFragment.html#setListAdapter(android.widget.ListAdapter)

所以你要么扩展ListFragment

扩展Fragment使用 ListView 为布局充气, 初始化 ListViewonCreateViewFragment然后使用 listView.setAdapter(adapter) .

如果你想扩展 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;
}

关于java - 无法解析方法 setListAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24076767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com