我正在实现一个BaseAdapter,但是没有调用它的getView()方法。这是我的简单适配器:
public class Adapter extends BaseAdapter {
Context context;
public Adapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
Log.d("log", "getcount");
return 2;
}
@Override
public Object getItem(int i) { return i; }
@Override
public long getItemId(int i) { return i; }
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
Log.d("log", "getview");
if (view == null) {
view = LayoutInflater.from(context).inflate(R.layout.item_menu, null);
}
TextView textView = (TextView) view.findViewById(R.id.tv);
textView.setText("test");
return view;
}
}
以下是我设置适配器的方法:
public class MenuFragment extends Fragment {
ListView mListView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_menu, container, false);
mListView = (ListView) view.findViewById(R.id.lv);
mListView.setAdapter(new Adapter(this.getActivity()));
return inflater.inflate(R.layout.fragment_menu, container, false);
}
}
在日志中,我可以看到getcount
但不是getview
,这意味着它从未被调用过。我可以在屏幕上看到ListView(它的背景颜色是红色的,所以它肯定不会被隐藏)。我究竟做错了什么?
答案 0 :(得分:4)
请返回视图,而不是再次给布局充气
替换
return inflater.inflate(R.layout.fragment_menu, container, false);
通过
return view;