1

I am successful in implementing Pull to refresh listview with already available library.NoW the problem is while refreshing, the already displayed data in listview disappears (blank while page) , and when done with refreshing the data gets displayed with updated data , which is not a expected result

public class HomeFragment extends Fragment implements OnClickListener{
private ListView listView;
PullToRefreshListView refreshable_listView;
private MyCustomAdapter mAdapter;
ArrayList<HashMap<String, String>> newsList;
String index_th="0";

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.listview_layout, container,false);

            //karthik
            refreshable_listView = (PullToRefreshListView) rootView.findViewById(R.id.listView);

            refreshable_listView.setOnRefreshListener(new OnRefreshListener<ListView>() {

                @Override
                public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                    // TODO Auto-generated method stub                                                                  
                    new GetList().execute(index_th);
                }


            });

            listView=refreshable_listView.getRefreshableView();
}
private class GetList extends AsyncTask<String, Void, Void> {
        Dialog dialog = new Dialog(getActivity());
        String jsonStr;
        ArrayList<HashMap<String, String>> Local_newsList;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Local_newsList=new ArrayList<HashMap<String,String>>();



            // Showing progress dialog
            /*dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout._wait_dialog);
            dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
            dialog.show();*/

        }

        @Override
        protected Void doInBackground(String... params) {

            if (NetworkCheck.isNetworkAvailable(getActivity()) == true) {

                //Log.d("index count for array", params[0]);
                if(Integer.parseInt(params[0])==0){
                    //Log.d("index count for array","Rweached");
                    newsList = new ArrayList<HashMap<String, String>>();
                }


                // Creating service handler class instance
                ServiceHandler sh = new ServiceHandler();

                // Making a request to url and getting response
                jsonStr = sh.makeServiceCall(All_link.HOME_DATA_URL + "/"+params[0]+"/"+ icount,
                        ServiceHandler.GET);

                Log.d("Response: ", "> " + jsonStr);

                if (jsonStr != null) {
                    try {
                        JSONObject jsonObj = new JSONObject(jsonStr);

                        // Getting JSON Array node
                        news = jsonObj.getJSONArray(All_link.TAG_NEWS);


                        err = jsonObj.getString(All_link.TAG_ERROR);
                        String more = jsonObj.getString(All_link.TAG_MORE);

                        Log.e("------>Error",String.valueOf(err));
                        Log.e("------>More",String.valueOf(more));
                        if(more.equals("0")){

                            load_more = 0;

                        }else{

                            load_more = 1;
                        }

                        // looping through All Contacts
                        for (int i = 0; i < news.length(); i++) {

                            //karthik



                            JSONObject segments_list = news.getJSONObject(i);

                            for (int plates_count = 0; plates_count < segments_list
                                    .length(); plates_count++) {


                                String plates = "";
                                if (plates_count == 3) {
                                    plates = String.valueOf("banner_image");

                                    JSONObject segments_plates = segments_list
                                            .getJSONObject(plates);

                                    //String flag = "banner_image";
                                    String id = segments_plates
                                            .getString(All_link.TAG_BANNER_ID);
                                    String banner_no = segments_plates
                                            .getString(All_link.TAG_BANNER_NO);

                                    String banner_image;

                                    if(segments_plates.getString(All_link.TAG_BANNER_THUMB_URL)==""){

                                        banner_image = All_link.TAG_NO_IMAGE;
                                    }else{

                                        banner_image = segments_plates.getString(All_link.TAG_BANNER_THUMB_URL);
                                    }

                                    String banner_status = segments_plates
                                            .getString(All_link.TAG_BANNER_STATUS);


                                    // tmp hashmap for single news
                                    HashMap<String, String> news_hashmap = new HashMap<String, String>();

                                    // adding each child node to HashMap key =>
                                    // value
                                    news_hashmap.put(All_link.TAG_BANNER_ID, id);
                                    news_hashmap.put(All_link.TAG_BANNER_NO,
                                            banner_no);
                                    news_hashmap.put(All_link.TAG_BANNER_THUMB_URL,
                                            banner_image);
                                    news_hashmap.put(All_link.TAG_BANNER_STATUS,
                                            banner_status);
                                    /*news_hashmap
                                            .put(All_link.TAG_BANNER_FLAG, flag);*/

                                    // adding contact to contact list



                                    newsList.add(news_hashmap);


                                    //karthik newsList

                                    //Local_newsList.add(news_hashmap);


                                    addSeparatorItem();
                                } else {
                                    plates = String.valueOf(plates_count + 1);

                                    JSONObject segments_plates = segments_list
                                            .getJSONObject(plates);

                                    if(segments_plates.getString(All_link.TAG_NEWS_TYPE).equals("2")){

                                        //type_of_news = segments_plates.getString(All_link.TAG_NEWS_TYPE);
                                        addSeparatorItem_for_live();
                                    }                                   

                                    String id = segments_plates.getString(All_link.TAG_ID);
                                    String news_title = segments_plates.getString(All_link.TAG_NEWS_TITLE);
                                    String news_desc = segments_plates.getString(All_link.TAG_DESC);
                                    String segment = segments_plates.getString(All_link.TAG_SEGMENT);
                                    String plate = segments_plates.getString(All_link.TAG_PLATE);

                                    String img ="";
                                    if(segments_plates.getString(All_link.TAG_THUMB_URL).equals("")){

                                        img = All_link.TAG_NO_IMAGE;

                                    }else{

                                        img = segments_plates.getString(All_link.TAG_THUMB_URL);
                                    }


                                    HashMap<String, String> news_hashmap = new HashMap<String, String>();

                                    // adding each child node to HashMap key =>
                                    // value
                                    news_hashmap.put(All_link.TAG_ID, id);
                                    news_hashmap.put(All_link.TAG_NEWS_TITLE, news_title);
                                    news_hashmap.put(All_link.TAG_DESC, news_desc);
                                    news_hashmap.put(All_link.TAG_SEGMENT, segment);
                                    news_hashmap.put(All_link.TAG_PLATE, plate);
                                    news_hashmap.put(All_link.TAG_THUMB_URL, img);
                                    //news_hashmap.put(All_link.TAG_BANNER_FLAG, flag);
                                    // adding contact to contact list

                                    newsList.add(news_hashmap);



                                    //karthik newsList

                                    //Local_newsList.add(news_hashmap);

                                    int val = 0;
                                    int val2 = 1;
                                    val = (Integer.parseInt(segment)%2)==0 ? val:val2;

                                    if((Integer.parseInt(segment)%2)!=0){
                                        Log.e("--->CHECKING ODD EVEN", String.valueOf(val));
                                        addSeparatorItem();
                                        addSeparatorItem_for_alternate();   
                                    }
                                }
                            }

                        }
                    } catch (JSONException e) {
                        Local_newsList=null;
                        e.printStackTrace();
                    }
                } else {
                    Log.e("ServiceHandler", "Couldn't get any data from the url");
                }
            } else {
                Log.e("Network Error", "Internet Connection Error");
                error_flag = 1;
                // error = "Internet Connection Error";

            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            getActivity().runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub

                    mAdapter = new MyCustomAdapter(getActivity(), newsList);                            

                    mAdapter.notifyDataSetChanged();
                    refreshable_listView.setAdapter(mAdapter);
                         refreshable_listView.onRefreshComplete();


                }
            });

            super.onPostExecute(result);            
}

MyCustomAdapter.java

private class MyCustomAdapter extends BaseAdapter {

        private Activity activity;
        Boolean result;
        private final LayoutInflater inflater = null;
        public ImageLoader imageLoader;

        private static final int TYPE_ITEM = 0;
        private static final int TYPE_SEPARATOR = 1;
        private static final int TYPE_SEPARATOR_ALTERNATE = 2;
        private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 3;
        private int live_type = 0;


        private LayoutInflater mInflater;

        public MyCustomAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
            mInflater = (LayoutInflater) a.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            activity = a;
            newsList=d;
            imageLoader = new ImageLoader(activity.getApplicationContext());
        }

        @Override
        public int getItemViewType(int position) {

            int separtorValue;
            int pos = mSeparatorsSet.contains(position) ? TYPE_SEPARATOR: TYPE_ITEM;
            live_type = mSeparatorsSet_live_layout.contains(position) ? TYPE_SEPARATOR: TYPE_ITEM;
            if(pos==1){
                //Log.e("--->if called- position->", String.valueOf(position));
                if(mSeparatorsSet_alternate_layout.contains(position)){

                    //Log.e("--->if called-->", String.valueOf(position));
                    separtorValue = 2;
                }else{

                    separtorValue = 1;  
                }


            }else{

                separtorValue = 0;
            }


            return separtorValue;
        }


        @Override
        public int getViewTypeCount() {
            Log.e("--->TYPE_MAX_COUNT called-->", String.valueOf(TYPE_MAX_COUNT));
            return TYPE_MAX_COUNT;
        }

        @Override
        public int getCount() {
            return newsList.size();
        }

        @Override
        public HashMap<String, String> getItem(int position) {
            return newsList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            NewsViewHolder holder = null;
            int type = getItemViewType(position);

            Log.e("--->SHISHIR-->", String.valueOf(live_type));

            /*System.out.println("getView " + position + " " + convertView
                    + " type = " + type);
**/
            //Log.e("--->SHISHIR-->", String.valueOf(position) + " -->live"+String.valueOf(mSeparatorsSet_live_layout.first()));



            if (convertView == null) {
                holder = new NewsViewHolder();

                 //Log.e("--itempostion", String.valueOf(getItem(type)));

                switch (type) {
                case TYPE_ITEM:




                    if (CURRENT_THEME==1) {
                        convertView = mInflater.inflate(
                                R.layout.theme_one_row_small, null);
                    }

                    else{
                        convertView = mInflater.inflate(
                                R.layout.theme_one_row_small_new, null);
                    }




                    holder.img_flag = (ImageView) convertView.findViewById(R.id.img_live_flag);
                    holder.id = (TextView) convertView.findViewById(R.id.fid);
                    holder.live = (TextView) convertView.findViewById(R.id.live);
                    holder.flag = (TextView) convertView.findViewById(R.id.flag);
                    holder.name = (TextView) convertView
                            .findViewById(R.id.title);

                    holder.img = (ImageView) convertView
                            .findViewById(R.id.list_image);

                    imageLoader
                            .DisplayImage(
                                    com.rb.library.All_link.IMAGE_URI_BANNER
                                            + newsList.get(position).get(
                                                    All_link.TAG_THUMB_URL),
                                    holder.img);

                    break;
                case TYPE_SEPARATOR_ALTERNATE:





                    if (CURRENT_THEME==1) {
                        convertView = mInflater
                                .inflate(R.layout.theme_one_row_big, null);
                    }

                    else{
                        convertView = mInflater
                                .inflate(R.layout.theme_one_row_big_new, null);
                    }



                    holder.flag = (TextView) convertView.findViewById(R.id.flag);

                    holder.img_flag = (ImageView) convertView.findViewById(R.id.img_live_flag);
                    holder.id = (TextView) convertView.findViewById(R.id.fid);
                    holder.live = (TextView) convertView.findViewById(R.id.live);
                    holder.name = (TextView) convertView
                            .findViewById(R.id.title);
                    holder.img = (ImageView) convertView
                            .findViewById(R.id.list_image);


                    break;
                case TYPE_SEPARATOR:
                    convertView = mInflater
                            .inflate(R.layout.theme_banner, null);
                    holder.flag = (TextView) convertView.findViewById(R.id.flag);
                    holder.id = (TextView) convertView.findViewById(R.id.fid);
                    holder.name = (TextView) convertView
                            .findViewById(R.id.title);
                    holder.img = (ImageView) convertView
                            .findViewById(R.id.list_image);

                    break;


                }


                convertView.setTag(holder);
            } else {
                holder = (NewsViewHolder) convertView.getTag();
            }

            switch (type) {
            case TYPE_ITEM:

                if(live_type == 1){

                    holder.img_flag.setVisibility(0);
                    holder.live.setText("live");

                }else{

                    holder.live.setText("not_live");
                    holder.img_flag.setVisibility(8);
                }
                imageLoader.DisplayImage(com.rb.library.All_link.IMAGE_URI+ newsList.get(position).get(All_link.TAG_THUMB_URL),
                        holder.img);
                holder.id.setText(String.valueOf(newsList.get(position).get(
                        All_link.TAG_ID)));
                holder.flag.setText("normal");

                break;
            case TYPE_SEPARATOR_ALTERNATE:

                if(live_type == 1){

                    holder.img_flag.setVisibility(0);
                    holder.live.setText("live");
                    //Log.e("--itempostion Live", "live");
                }else{

                    holder.live.setText("not_live");
                    holder.img_flag.setVisibility(8);
                }

                imageLoader.DisplayImage(com.rb.library.All_link.IMAGE_URI+ newsList.get(position).get(All_link.TAG_THUMB_URL),
                        holder.img);
                holder.id.setText(String.valueOf(newsList.get(position).get(
                        All_link.TAG_ID)));
                holder.flag.setText("normal");

                Log.e("karthik", "karthik "+com.rb.library.All_link.IMAGE_URI+ newsList.get(position).get(All_link.TAG_THUMB_URL));

                break;
            case TYPE_SEPARATOR:
                Log.e("--banner Images", com.rb.library.All_link.IMAGE_URI_BANNER
                        + newsList.get(position).get(All_link.TAG_BANNER_THUMB_URL));
                imageLoader.DisplayImage(com.rb.library.All_link.IMAGE_URI_BANNER
                        + newsList.get(position).get(All_link.TAG_BANNER_THUMB_URL),
                        holder.img);
                holder.id.setText(String.valueOf(newsList.get(position).get(
                        All_link.TAG_BANNER_ID)));
                holder.flag.setText(newsList.get(position).get(All_link.TAG_BANNER_THUMB_URL));
                break;
            }





            holder.name.setText(String.valueOf(newsList.get(position).get(All_link.TAG_NEWS_TITLE)));

            return convertView;
        }

    }
6
  • I have no problem with linking, but its better to delete the link and just copy-paste your current code again here. People sometimes downvote a lot
    – Blaze Tama
    Nov 15, 2014 at 9:14
  • @Blaze Tama --thanks for alerting me. Nov 15, 2014 at 9:16
  • Look here MyCustomAdapter(Activity a, ArrayList<HashMap<String, String>> d) you passed d to your constructor, but you never use it?
    – Blaze Tama
    Nov 15, 2014 at 9:18
  • @Blaze Tama -- can u please be informative Nov 15, 2014 at 9:23
  • Sorry what do you mean? Im asking about your adapter's constructor
    – Blaze Tama
    Nov 15, 2014 at 9:26

1 Answer 1

1

My guess is because you call setAdapter everytime you pull to refresh, in onPostExecute :

mAdapter = new MyCustomAdapter(getActivity(), newsList);                            
mAdapter.notifyDataSetChanged();
refreshable_listView.setAdapter(mAdapter);
refreshable_listView.onRefreshComplete();

Try to move :

mAdapter = new MyCustomAdapter(getActivity(), newsList);                            
refreshable_listView.setAdapter(mAdapter);

To your onCreateView because you only need to initialize and set the adapter once.

You just need to call those in your onPostExecute, because you called newsList.add in the onBackground :

mAdapter.notifyDataSetChanged();
refreshable_listView.onRefreshComplete();

The above's code will only change the data on your ListView, without re-initializing anything.

3
  • when I try to move mAdapter = new MyCustomAdapter(getActivity(), newsList); refreshable_listView.setAdapter(mAdapter); to onCreate() , it throws NullPointerException. Nov 15, 2014 at 10:03
  • Dont forget to initialize your newsList
    – Blaze Tama
    Nov 15, 2014 at 10:06
  • I am very sorry for late reply ..It took some time for me to check .. Sometime it seems to be fine and some other time it disappears .I don't understand the problem Nov 17, 2014 at 5:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.