Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listview in SwipeRefreshLayout - does not show anything when custom adapter have only one row to show

enter image description here

In my app I have a listview. all things work perfectly when i have more than one item to show, but when I have one item to show in my listview, it show me a rare behave.

First: if that single item height be greater than window height...it act like a charm and work properly.(A)

Second: if that single item height be smaller than window height...sometimes it show the item (B) and sometimes does not! (C).

Third: I'm using SwipeRefreshLayoutfrom support library, when i try to refresh listview, if I have one item in listview... it does not show me the item if its height be smaller than window height.(C)

I can't figure it out!! Can any body help me

I know that this issue related to height of SwipeRefreshLayout or Listview, cause when I set marginTop to this view ( as half of screen height), listview works perefctly.

My xml file is:

<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_Container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="0dp"
android:background="#FFedf1f2"
android:paddingLeft="8.5dp"
android:paddingRight="8.5dp"
android:paddingTop="0dp" >

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:divider="@android:color/transparent"
    android:dividerHeight="8.5dp"
    android:scrollbars="none" >
</ListView>

Everything works as a charm until I've at least more then one Item to show in listview. but when I've one item to show in list view, My listview does not show anything :(

Why? where is my fault? Where could I be wrong?

this is getView() of my adapter ( extended BaseAdapter - I've cut some line of codes ):

public View getView(int position, View convertView, ViewGroup parent) {

    Typeface MyriadRegular  = Farsi.GetMyriadRegular(mActivity);
    Typeface MyriadBold     = Farsi.GetMyriadBold(mActivity);
    Typeface Yagut          = Farsi.GetYagut(mActivity);
    Typeface JalalBold      = Farsi.GetJalalBold(mActivity);

    User_Session            = new SessionManagement(mActivity);

    View row    = convertView;
    ViewHolder holder;

    mVideo              = mData.get(position);
    //======================================================================== USER
    USERNAME            = mVideo.get("USERNAME").toUpperCase();
    CHANNEL_NAME        = mVideo.get("CHANNEL_NAME");
    CHANNEL_ID          = mVideo.get("CHANNEL_ID");
    USER_ID             = mVideo.get("USER_ID");
    AVATAR              = mVideo.get("AVATAR");
    //======================================================================== VIDEO
    VIDEO_URL           = mVideo.get("VIDEO_URL");
    TITLE               = mVideo.get("TITLE");
    DESCRIPTION         = mVideo.get("DESCRIPTION");
    BIG_IMAGE           = mVideo.get("BIG_IMAGE");
    VIEW_CNT            = mVideo.get("VIEW_CNT");
    COMMENT_CNT         = mVideo.get("COMMENT_CNT");
    LIKE_CNT            = mVideo.get("LIKE_CNT");
    LIKE_COUNT          = Integer.parseInt(LIKE_CNT);
    DISLIKE_CNT         = mVideo.get("DISLIKE_CNT");
    DISLIKE_COUNT       = Integer.parseInt(DISLIKE_CNT);
    POST_DATE           = mVideo.get("POST_DATE");
    DURATION            = mVideo.get("DURATION");
    CATEGORY_TITLE      = mVideo.get("CATEGORY_TITLE");
    CATEGORY_ID         = mVideo.get("CATEGORY_ID");
    VIDEO_ID            = mVideo.get("VIDEO_ID");
    VIDEO_WEB           = mVideo.get("VIDEO_WEB");
    LIKE_W_USER         = mVideo.get("LIKE_W_USER");
    DISLIKE_W_USER      = mVideo.get("DISLIKE_W_USER");
    Follow_B_USER       = mVideo.get("FOLLOW_B_USER");
    Like_with_user      = Boolean.parseBoolean(LIKE_W_USER);
    Dislike_with_user   = Boolean.parseBoolean(DISLIKE_W_USER);
    FOLLOW_BY_USER      = Boolean.parseBoolean(Follow_B_USER);


    if(convertView == null) {
        row     = mInflater.inflate(R.layout.layout_list_row, parent, false);
        holder  = new ViewHolder(row);
        row.setTag(holder);

    }else{
        row     = convertView;
        holder  = (ViewHolder) row.getTag();
    }

    holder.user_Avatar.setTag(USER_ID);
    holder.user_Name.setTag(USER_ID);

    holder.user_Avatar.setOnClickListener(ChannelProfile);
    holder.user_Name.setOnClickListener(ChannelProfile);

    holder.video_Comment.setTag(position);

    holder.video_Like.setTag(R.string.Position, position);
    holder.video_Like.setTag(R.string.Video_ID, VIDEO_ID);

    holder.video_Dislike.setTag(R.string.Position, position);
    holder.video_Dislike.setTag(R.string.Video_ID, VIDEO_ID);

    holder.video_Edit.setTag(position);
    holder.video_Delete.setTag(position);

    holder.video_editText.setTypeface(JalalBold);
    holder.video_deleteText.setTypeface(JalalBold);


    if (!mNext.equals("NONE")){
        if (position >= getCount() - 1){
            if(mSwipe != null){
                mSwipe.setRefreshing(true);
            }

            loadmore();
        }
    }



    holder.video_Duration.setTypeface(MyriadBold);
    holder.video_Duration.setText(Farsi.Convert(DURATION));

    holder.video_Title.setTypeface(Yagut);
    holder.video_Title.setText(TITLE);

    holder.user_Name.setTypeface(MyriadRegular);
    holder.user_Name.setText(CHANNEL_NAME);

    holder.video_Description.setTypeface(MyriadRegular);
    holder.video_Description.setText(DESCRIPTION);

    holder.video_Description.setClickable(true);

    holder.video_CategoryTitle.setTypeface(JalalBold);
    holder.video_CategoryTitle.setText(CATEGORY_TITLE);

    holder.video_ViewCount.setTypeface(JalalBold);
    holder.video_ViewCount.setText(VIEW_CNT);

    holder.video_CommentCount.setTypeface(JalalBold);



    return row;
}

Edite: and this is my viewHolder class:

static class ViewHolder{

    TextView            video_Duration;
    TextView            video_Title;
    ExpandableTextView  video_Description;
    TextView            video_CategoryTitle;
    TextView            video_ViewCount;
    TextView            video_PostDate;
    TextView            video_LikeCount;
    TextView            video_CommentCount;
    TextView            video_DislikeCount;
    TextView            video_editText;
    TextView            video_deleteText;
    ImageView           video_BigImage;
    Button              video_Like;
    Button              video_Dislike;
    Button              video_Comment;
    RelativeLayout      video_options;
    RelativeLayout      video_Edit;
    RelativeLayout      video_Delete;

    //=================================
    TextView            user_Name;
    ImageView           user_Avatar;

    public ViewHolder(View vi) {
        video_Duration      = (TextView) vi.findViewById(R.id.video_Duration);
        video_Title         = (TextView) vi.findViewById(R.id.video_Title);
        video_Description   = (ExpandableTextView) vi.findViewById(R.id.video_Description);
        video_CategoryTitle = (TextView) vi.findViewById(R.id.video_Category_Title);
        video_ViewCount     = (TextView) vi.findViewById(R.id.video_View_Count);
        video_PostDate      = (TextView) vi.findViewById(R.id.video_Post_Date);
        video_LikeCount     = (TextView) vi.findViewById(R.id.video_like_cnt);
        video_CommentCount  = (TextView) vi.findViewById(R.id.video_Comment_cnt);
        video_DislikeCount  = (TextView) vi.findViewById(R.id.video_dislike_cnt);
        video_editText      = (TextView) vi.findViewById(R.id.edit_text);
        video_deleteText    = (TextView) vi.findViewById(R.id.delete_text);
        video_BigImage      = (ImageView) vi.findViewById(R.id.video_Image);
        video_Like          = (Button) vi.findViewById(R.id.video_Like);
        video_Dislike       = (Button) vi.findViewById(R.id.video_disLike);
        video_Comment       = (Button) vi.findViewById(R.id.video_Comment);
        video_options       = (RelativeLayout) vi.findViewById(R.id.videoOptions);
        video_Edit          = (RelativeLayout) vi.findViewById(R.id.videoEditBtn);
        video_Delete        = (RelativeLayout) vi.findViewById(R.id.videoDeleteBtn);
        user_Name           = (TextView) vi.findViewById(R.id.username);
        user_Avatar         = (ImageView) vi.findViewById(R.id.user_avatar);


    }
}

JSON Parsing Portion ( I'm using ION Library for Asynchronous Networking):

    private void initSearch(String tag, boolean refresh){

    final boolean ReforNormal = refresh;

    URL = Constants.Search_URL+tag;

    if(User_Session.isLoggedIn()){
        URL = Constants.Search_URL+tag+"&token="+User_TOKEN;
    }

    System.out.println("URL: "+URL);

    if (VideoLoad != null && !VideoLoad.isDone() && !VideoLoad.isCancelled()) return;

    VideoLoad   = Ion.with(this).load(URL).asJsonObject().setCallback(new FutureCallback<JsonObject>() {

        @Override
        public void onCompleted(Exception e, JsonObject result) {

            if (ReforNormal == true){
                mAdapter.clearData();
            }

            if ( e == null){

                System.out.println("RESULT: "+result);
                Videos  = result.getAsJsonArray("objects");
                if(Videos.size() > 0){

                    for (int i = 0; i < Videos.size(); i++) {

                        JsonObject Item = Videos.get(i).getAsJsonObject();
                        //======================================================================== USER
                        USERNAME        = Item.get("username").getAsString();
                        CHANNEL_NAME    = Item.get("channel_name").getAsString();
                        CHANNEL_ID      = Item.get("channel_id").getAsString();
                        USER_ID         = Item.get("user_id").getAsString();
                        AVATAR          = Item.get("avatar").getAsString();
                        //======================================================================== VIDEO
                        VIDEO_URL       = Item.get("video_url").getAsString();
                        TITLE           = Item.get("title").getAsString();
                        DESCRIPTION     = Item.get("text").getAsString();
                        BIG_IMAGE       = Item.get("image").getAsString();
                        VIEW_CNT        = Item.get("cnt_view").getAsString();
                        COMMENT_CNT     = Item.get("cnt_comment").getAsString();
                        LIKE_CNT        = Item.get("cnt_like").getAsString();
                        DISLIKE_CNT     = Item.get("cnt_dislike").getAsString();
                        POST_DATE       = Item.get("create_time").getAsString();
                        DURATION        = Item.get("duration").getAsString();
                        CATEGORY_TITLE  = Item.get("category_title").getAsString();
                        CATEGORY_ID     = Item.get("category_id").toString();
                        VIDEO_ID        = Item.get("id").getAsString();
                        VIDEO_WEB       = Item.get("web_url").getAsString();
                        LIKE_W_USER     = Item.get("like_with_user").getAsString();
                        DISLIKE_W_USER  = Item.get("dislike_with_user").getAsString();
                        Follow_B_USER   = Item.get("follow_by_user").getAsString();

                        HashMap<String, String> Items = new HashMap<String, String>();
                        Items.put("USERNAME", USERNAME);
                        Items.put("CHANNEL_NAME", CHANNEL_NAME);
                        Items.put("CHANNEL_ID", CHANNEL_ID);
                        Items.put("USER_ID", USER_ID);
                        Items.put("AVATAR", AVATAR);
                        Items.put("VIDEO_URL", VIDEO_URL);
                        Items.put("TITLE", TITLE);
                        Items.put("DESCRIPTION", DESCRIPTION);
                        Items.put("BIG_IMAGE", BIG_IMAGE);
                        Items.put("VIEW_CNT", VIEW_CNT);
                        Items.put("COMMENT_CNT", COMMENT_CNT);
                        Items.put("LIKE_CNT", LIKE_CNT);
                        Items.put("DISLIKE_CNT", DISLIKE_CNT);
                        Items.put("POST_DATE", POST_DATE);
                        Items.put("DURATION", DURATION);
                        Items.put("CATEGORY_TITLE", CATEGORY_TITLE);
                        Items.put("CATEGORY_ID", CATEGORY_ID);
                        Items.put("VIDEO_ID", VIDEO_ID);
                        Items.put("VIDEO_WEB", VIDEO_WEB);
                        Items.put("LIKE_W_USER", LIKE_W_USER);
                        Items.put("DISLIKE_W_USER", DISLIKE_W_USER);
                        Items.put("Follow_B_USER", Follow_B_USER);

                        DATA.add(Items);
                    }

                    Page            = result.getAsJsonObject("meta");
                    if(User_Session.isLoggedIn()){
                        HashMap<String, String> user    = User_Session.GetUserDetails();
                        String User_TOKEN               = user.get(SessionManagement.KEY_USERTOKEN);

                        if(Page.equals("")){
                            NextPage    = "NONE";
                        }else{
                            NextPage    = Page.get("next").getAsString()+"&token="+User_TOKEN;
                        }

                    }else{

                        if(Page.equals("")){
                            NextPage    = "NONE";
                        }else{
                            NextPage    = Page.get("next").getAsString();
                        }
                    }


                    mAdapter = new MyCustomAdapter(SearchActivity.this, DATA, NextPage, ParsvidswipeLayout, false, false, "");
                    list.setAdapter(mAdapter);
                    mAdapter.notifyDataSetChanged();
                    new Handler().postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            ParsvidswipeLayout.setRefreshing(false);
                        }
                    }, 500);

                }else{
                    new Handler().postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            ParsvidswipeLayout.setRefreshing(false);
                        }
                    }, 500);
                }


            }
        }
    });
}
like image 840
MAY3AM Avatar asked Oct 20 '22 23:10

MAY3AM


1 Answers

Problem is here where you are creating new Adapter each time rather than adding data to the Adapter

mAdapter = new MyCustomAdapter(SearchActivity.this, DATA, NextPage, ParsvidswipeLayout, false, false, ""); 
                    list.setAdapter(mAdapter); 
                    mAdapter.notifyDataSetChanged(); 

Just add you data using method addAll( ) in your adapter. There is totally no need to change adapter again and again on every data load.

I found only this problem in your code.

like image 197
AZ_ Avatar answered Oct 23 '22 08:10

AZ_