2

I never seen something like that before but from last few days I am experiencing very peculiar behavior of the Listview and until now I am not been able to isolate the issue.

I only paste the code which I think is necessary and later I will tell you my problem.

/* tell adapter that data is done and stop the more loading progress bar*/ 
public void setDataChanged(boolean type)
{
    isLoadingData = false;
    notifyDataSetChanged();
}

/* If loading is going on size of the adapter will increase to show the additional progress bar*/ 
@Override
public int getCount() {
    int size = friendsModels.size();
    if (isLoadingData) {
        size += 1;
    }
    Log.i("size", String.valueOf(size));//to check size

    return size;
}
/* set loading true and page number of the data items*/ 
public void setLoadingData(boolean isLoadingData, int page) {
    this.isLoadingData = isLoadingData;
    this.page = page;
    notifyDataSetChanged();
}

/* MAX_ITEM number of item returning from rest webservice per page*/ 
@Override
public int getItemViewType(int position) {
    if(isLoadingData && position%MAX_ITEM==0 && position>0)
    {
        if(position/MAX_ITEM==page+1)
        return 1;
    }
    return 0;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    Log.e("getView", "getView");

    EventHolder eventHolder;
    int type = getItemViewType(position);
    LayoutInflater li = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if(convertView==null){
        eventHolder = new EventHolder();
        if(type==0)
        {
            convertView = li.inflate(R.layout.friends_list_items, parent,false);

            eventHolder.name   = (TextView)convertView.findViewById(R.id.textview_friend_name);

            convertView.setTag(eventHolder);

        }
        else if(type==1)
        {
            convertView = li.inflate(R.layout.progress_dialog, parent,false);
            eventHolder.progress = (RelativeLayout)convertView.findViewById(R.id.progress_layout);
            convertView.setTag(eventHolder);
        }
    }
    else
    {
        eventHolder = (EventHolder) convertView.getTag();
        if(type==0)
        {
            setFriends(eventHolder, position);
        }
    }

    return convertView;
}

Now onScroll method-

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {
    if(firstVisibleItem + visibleItemCount >= totalItemCount && !loading && totalItemCount%6==0 && totalItemCount>0 && NetworkUtil.isNetworkAvailable(activity))
    {
        loading = true;
        friendsAdapter.setLoadingData(true,pageNo);
        ControllerRequests.getPeople(FragmentClass.this, ++pageNo, search.getText().toString());
    }
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {       
}

Though everything is working fine when there are like 5 or more items but as the item size decreases let's say 1 or 2 then sometimes the getView method don't get called though log info is showing me getCount = 1 or 2 but getView just don't get called. There is no pattern, I mean sometimes 5 times getView get called it works fine then suddenly not and like that.

mihirjoshi
  • 12,161
  • 7
  • 47
  • 78
  • If getView is not getting called, then you should not see any content on screen. Question: does 1 or 2 items occupy the entire screen (or more ) or whether the content is fitted inside the visible portion of screen? – AjOnFire Sep 25 '13 at 14:50
  • Exactly I am not seeing anything on the screen its just blank. Visible portion of the screen is filed with 6 items and 1 or 2 items just occupy the part of the screen. – mihirjoshi Sep 25 '13 at 16:22

1 Answers1

1

This is a strange check:

if(firstVisibleItem + visibleItemCount >= totalItemCount && !loading &&
                totalItemCount%6==0 && totalItemCount>0 && 
                                  NetworkUtil.isNetworkAvailable(activity))

I am referring to:

totalItemCount % 6 == 0

So, unless the total number of items will always be a multiple of 6, this check will prevent friendsAdapter.setLoadingData(true,pageNo); from being called. That's why the statement notifyDataSetChanged(); that resides inside setLoadingData(boolean, int) will not be executed whenever totalItemCount % 6 != 0.

Edit:

I also cannot think of a situation where this will be true:

firstVisibleItem + visibleItemCount > totalItemCount

You can go with the following to check if the user has reached the end of the list:

firstVisibleItem + visibleItemCount == totalItemCount
Vikram
  • 51,313
  • 11
  • 93
  • 122
  • I will surely try that but the thing is I don't have any problem when items are 6 or more, problem occurs when there are only lets say 1 or 2 items. So `setLoadingData(boolean, int)` won't get called in that case. – mihirjoshi Sep 26 '13 at 04:57
  • 1
    @mjosh `setLoadingdata(boolean, int)` won't be called unless your adapter holds: 0, 6, 12, 18 ... items. On any other values, the `if check` will not pass. The problem is still present when you have more than 6 items. Try adding this statement `Log.d("IF_CHECK", "if check passed")` inside the if-block. Run your application with with a few different test cases: 1 item, 6 items, 20 items, 24 items. You'll notice that the Log statement won't be outputted even with 20 items. Can I ask why you are performing this check? – Vikram Sep 26 '13 at 05:13
  • see `setLoadingdata(boolean, int)` will only get executed when there are more than 6 items because the server is giving 6 items per `page`. – mihirjoshi Sep 26 '13 at 15:42
  • @mjosh I see. So, the problem is that when the server returns only 1 or 2 items (instead of 6), those 1 or 2 items are not even displayed. Could you share the code where the objects are fetched from the server and passed on to the adapter? – Vikram Sep 26 '13 at 17:56
  • Code is proprietary of the company.. all I can tell you that `getCount()` is showing 1 or 2 in the `log`. – mihirjoshi Sep 26 '13 at 18:16
  • Where is @romainguy when you need him. – mihirjoshi Oct 01 '13 at 17:59
  • 1
    @mjosh Probably baking version 4.4. – Vikram Oct 01 '13 at 19:48