我希望有一个自定义行,所以我使用的XML列表视图和膨胀成一个片段。我很困惑如何设置列表视图的适配器。 我创建了一个新的适配器,它扩展底座适配器。在getView方法,我真的不知道是什么背景,而充气row.xml布局传递。 如何设置列表视图的适配器和在哪里?
公共类ResultsFragment扩展片段{
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
视图V = inflater.inflate(R.layout.results_layout,集装箱,假);
ListView控件=(ListView控件)v.findViewById(R.id.results);
返回伏;
}
@覆盖
公共无效onActivityCreated(包savedInstanceState){
super.onActivityCreated(savedInstanceState);
loadPage(dataBean.getWhat(),dataBean.getWhere(),dataBean.getPageStart());
//resultsAdapter.setRssData(rssData);
// setListAdapter(resultsAdapter);
}
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
上下文的背景下= getActivity()getApplicationContext()。
resultsAdapter =新ResultsAdapter(上下文);
}
/ **
*设置列表适配器
* /
私人无效setAdapter(){
如果(listView.getAdapter()== NULL){
listView.setAdapter(resultsAdapter);
}
其他{
resultsAdapter.notifyDataSetChanged();
}
}
}
解决方案
您必须扩展(的 Listfragment
,而不是片段
),并利用其 ListFragment.setListAdapter
来设置您的适配器。在适配器 getView()
夸大你行..这一切都
I want a custom row, so I am using a List View in an xml and inflating into a fragment. I am very confused of how to set the adapter for the list View. I created a new adapter which extends Base Adapter. In the getView method, I really don't know what context to pass while inflating the row.xml layout. How do I set the adapter for the list view and where?
public class ResultsFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.results_layout, container, false);
listView = (ListView)v.findViewById(R.id.results);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
loadPage(dataBean.getWhat(), dataBean.getWhere(), dataBean.getPageStart());
//resultsAdapter.setRssData(rssData);
//setListAdapter(resultsAdapter);
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Context context = getActivity().getApplicationContext();
resultsAdapter = new ResultsAdapter(context);
}
/**
* Set List Adapter
*/
private void setAdapter(){
if(listView.getAdapter() == null){
listView.setAdapter(resultsAdapter);
}
else{
resultsAdapter.notifyDataSetChanged();
}
}
}
解决方案
You must extend Listfragment
(instead of Fragment
), and using its ListFragment.setListAdapter
to set your adapter. In the adapter getView()
inflate your row.. that s all