0

Trying to Sync complete List, by doing tap on button, but instead of syncing whole list code only syncing records visible in a list, my mean when i do scroll down to list, it is not syncing records.

For example, I have 300 records in a list, but only 10 records are visible to camera screen for rest need to scroll, so my program only Syncing those 10 records, not whole list, why ?

    ImageButton buttonSync = (ImageButton) findViewById(R.id.sync_btn);
    buttonSync.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

             for(int i=0; i<lstView.getChildCount(); i++)
             {
                 startUpload(i);
             }          
        }
    });
Sun
  • 6,768
  • 25
  • 76
  • 131

1 Answers1

1

The getChildCount() method returns only the total number of children currently visible in the listview. You will have to get the data to be synced from the data-source (provided to the listview adapter). That is the correct way of doing it.

However, if you have to sync an item in the listview and update that particular view after syncing it, you can leverage the adapter's notifyDataSetChanged. You can have a flag that is checked to see if that particular record of the listview is to be updated or not.

// An array of flags, as many as the number of records in the listview
// such that, flag[0] is set to true to indicate that the first item in the
// listview needs to call startUpload()
private SparseBooleanArray flags = new SparseBooleanArray();

// At onClick, set all the flags to indicate that some data needs to be synced
ImageButton buttonSync = (ImageButton) findViewById(R.id.sync_btn);
    buttonSync.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

             for(int position=0; position<listView.getAdapter().getCount(); position++)
             {
                 flags.put(position, true);
             }

             // Calling this would ensure a call to getView() on every
             // visible child of the listview. That is where we will check if
             // the data is to be synced and displayed or not
             ((BaseAdapter) listView.getAdapter()).notifyDataSetChanged();
        }
    });

@Override
// In getView of the listview's adapter
public View getView(int position, View convertView, ViewGroup parent) {

    // If this item is to be synced
    if(flags.get(position)) {
        startUpload();

        // Mark as synced
        flags.put(position, false);
    }

    // Rest of the method that draws the view....
}
Srikanth
  • 2,014
  • 19
  • 22
  • getting: The method notifyDataSetChanged() is undefined for the type ListAdapter I posted edited code – Sun Feb 01 '14 at 09:01
  • Make sure that your adapter is a BaseAdapter and add a cast to that line as ((BaseAdapter) listView.getAdapter()).notifyDataSetChanged(); – Srikanth Feb 01 '14 at 09:17
  • Hope you understood what my suggestion was. If not, please understand that logic first. Then, include logs & markers to check the flow, instead of having binary "working" & "not working" descriptions. :) – Srikanth Feb 01 '14 at 09:51
  • again i need your help, check this: http://stackoverflow.com/questions/24731838/how-to-upload-multiple-images-one-by-one i used your code, but i need a small change, still whenever i am uploading/syncing images, its uploading all at same time, but I want to upload multiple images one by one, like once user do tap on upload all button ... – Sun Jul 16 '14 at 12:07
  • bro your help much needed please help once: http://stackoverflow.com/questions/24731838/how-to-upload-multiple-images-one-by-one – Sun Jul 17 '14 at 08:00