0xSina
0xSina

Reputation: 21553

Android getView() in BaseAdapter not called (but getCount() is called)

I am implementing a BaseAdapter, however it's getView() method is not called. Here's my simple adapter:

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;
    }
}

Here's how I set the adapter:

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);
    }
}

In the logs, I can see getcount but not getview which means it's never called. I can see the ListView on my screen (it's background color is red, so it's certainly not hidden). What am I doing wrong?

Upvotes: 0

Views: 1669

Answers (1)

Syed Waqas
Syed Waqas

Reputation: 862

please return view instead of inflating the layout again

replace

return inflater.inflate(R.layout.fragment_menu, container, false);

by

return view;

Upvotes: 4

Related Questions