Custom Adapter getView() method is not called

后端 未结 6 1159
悲&欢浪女
悲&欢浪女 2020-11-22 09:49

Here is the code of the fragment in which I am setting a custom adapter to the list.

There no errors but the ListView is empty. I have implemented

相关标签:
6条回答
  • 2020-11-22 09:49

    What you have been doing is

    In your adapter

    public CarListAdapter(Context context , ArrayList<CarDetail> items) {
    
        this.context = context;
        this.items = items;
    
    }
    

    in your Fragment

    adapter = new CarListAdapter(getActivity(),ServiceCarListFragment.this.carDetailList);
    

    I hope you will be using FragmentActivity

    You need to call

    adapter = new CarListAdapter(YOUR_ACTIVITY_CONTEXT, carDetailList);
    

    where YOUR_ACTIVITY_CONTEXT will be your FragmentActivity

    0 讨论(0)
  • 2020-11-22 09:53

    you must verify that the list has elements might have an error when adding items to your list . To verify , use the method:

    adapter.getCount();
    
    0 讨论(0)
  • 2020-11-22 09:57

    I had the same problem. And after trying all tips above my getView was still not being called. So I tried to remove the ScrollView that I used outside the ListView. Then the getView worked well. Just for add one more posibility. I Hope help someone.

    0 讨论(0)
  • 2020-11-22 10:03

    You are missing the super class in the constructor. See my example below:

    public AppDataAdapter(Activity a, int textViewResourceId, ArrayList<AppData> entries) {
        super(a, textViewResourceId, entries);
        this.entries = entries;
        this.activity = a;
    }
    
    0 讨论(0)
  • 2020-11-22 10:10

    The only reasons getView is not called are:

    1. getCount returns 0.
    2. you forget to call setAdapter on the ListView.
    3. If the ListView's visibility (or its container's visibility) is GONE. Thanks to @TaynãBonaldo for the valuable input.
    4. ListView is not attached to any viewport layout. That is, mListView = new ListView(...) is used without myLayout.addView(mListView).

    In the onPostExcute, after you create a new instance of CarListAdapter I will suggest you to update the new instance to your ListView. Indeed you need to call again

     mList.setAdapter(adapter);
    

    Edit: setAdapter should be always called on the ui thread, to avoid unexpected behaviours

    Edit2:

    The same applies to RecyclerView. Make sure that

    • getItemCount is returning a value grater than 0 (usually the dataset size)
    • both setLayoutManager and setAdapter have to be called on the UI Thread
    • The visibility of the widget has to be set to VISIBLE
    0 讨论(0)
  • 2020-11-22 10:13

    I faced similar problem. Here is a simple work around to solve it:

    In your onCreateView, you will have to wait before the view gets created. So change your lines from this:

    mList = (ListView)v.findViewById(R.id.list);
    mList.setAdapter(adapter);
    

    CHANGE THE ABOVE TWO LINES INTO:

    mList = (ListView)v.findViewById(R.id.list);
    mList.post(new Runnable() {
        public void run() {
            mList.setAdapter(adapter);
        }
    });
    

    Hope this will help others who would run into similar problem

    0 讨论(0)
提交回复
热议问题