Search code examples
androidlistviewnetwork-connection

Android: Handling network connectivity and listview


Hi i am not sure about implementing this particular scenario, i need some suggestions on it. i have a listview and i will be updating it by making a server call, if it is a small amount of data it will be quick, what if i have some 5000 records? do i need to load them in the memory or database and then query it and update listview? what is the best practice for this?

Suppose when i am downloading the data and suddenly there is a network drop? then how will i handle the data and updating the listview?


Solution

  • I'm not sure about 5K records, but ListViews are known to handle well even large number of records using an effective recycling mechanism. See this thread for a detailed explanation.

    Now, do yourself a favor and use Volley for your network resource loading. It does far better job in async loading of records into lists than most of us... In any case either the library you use or your own code must perform the resource loading/networking from a background thread.

    You should also apply the ViewHolder pattern to your code. Using a ViewHolder is always a hood idea. it becomes crucial for a really large number of records. Please use it.
    It should look like this

    class ViewHolder {
        textView text1;
        Button button1;
    }
    
    class MyAdapter {
        public View getView(int position, View convertView, ViewGroup parent) {
             ViewHolder viewHolder = null;
             if (convertView==null){
                  LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
                  convertView = inflater.inflate(layoutResourceId, parent, false);
                  viewHolder = new ViewHolderItem();
                  viewHolder.text1 = (TextView) convertView.findViewById(R.id.text1);
                  viewHolder.button1 = (Button) convertView.findViewById(R.id.button1);
                  ..........
                  convertView.setTag(viewHolder);
             }
             viewHolder = (ViewHolderItem) convertView.getTag();
             ObjectItem objectItem = data[position];
             ..........
        }
    }
    


    And, finally, if you really need partial loading together with a Load more functionality, please read: http://www.androidhive.info/2012/03/android-listview-with-load-more-button/