0

I have searched multiple websites, android developer sites and after no luck to my issue had to seek the community help. All help and suggestion will be highly appreciated.

ISSUE :

I have in total 12 images that I receive from the server (Parse in here) and I show each of them in a PagerAdapter. If all the values are not null the adapter code works fine, the twist is I allow null values to be stored in server. When I get the whole list back from server, I just want to have those views in adapter which contains not null. Example : Suppose 5 null paths I get then my adapter shows in total 12 views (7 with images, rest as blank pages).

My Adapter Code:

public class ProfileImageAdapter extends PagerAdapter implements OnTouchListener{

    private Context localContext;
    private LayoutInflater inflater;
    private List<DataModel> parseObjects = new ArrayList<DataModel>();
    private List<Integer> res = new ArrayList<Integer>();
    // Declare Variables
    private ImageView viewPagerDisplayImage;

    /**
     * 
     * @param context : The context where to display
     * @param parseObjects : The ParseObject to work with
     */
    public ProfileImageAdapter(Context context, List<DataModel> parseObjects) {
        this.localContext = context;
        this.parseObjects = parseObjects;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return (view == ((LinearLayout) object) && object != null);
    }

    /**
     * What is the item to show
     */
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        inflater = (LayoutInflater) localContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.profile_image_item, container, false);

        // Locate the Image View in viewpager_item.xml
        viewPagerDisplayImage = (ImageView) itemView.findViewById(R.id.view_pager_display_image_view);

        // Getting the image
        try {
            ParseFile imageFile = parseObjects.get(position).getImage();
            if (imageFile != null) {
                byte[] bitmapImageData = imageFile.getData();
                Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapImageData, 0, bitmapImageData.length);
                viewPagerDisplayImage.setImageBitmap(bitmap);
                viewPagerDisplayImage.setOnTouchListener(this);
                // Add viewpager_item.xml to ViewPager

                itemView.setTag("VALID");
                ((ViewPager) container).addView(itemView);
            } else {
                ((ViewPager) container).addView(itemView);
                itemView.setTag("INVALID");
                destroyItem(container, position, itemView);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // Remove viewpager_item.xml from ViewPager
        //((ViewPager) container).invalidate();
        ((ViewPager) container).removeView((View) object);
    }
}

My PagerActivity

public class PageViewerProfileImages  extends Activity {

    private ViewPager viewPager;
    private ProfileImageAdapter profileImageAdapter;
    private UserModel userModel = new UserModel();

    // Create the Page View for the Profile Images
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profile_pager);

        List<DataModel> parseObjects = userModel.getDataModels();
        viewPager = (ViewPager) findViewById(R.id.pagerProfile);
        profileImageAdapter =  new ProfileImageAdapter(this, parseObjects);
        // Setting the adapter
        viewPager.findViewWithTag("VALID");
        viewPager.setAdapter(profileImageAdapter);
    }

}

My references:

  1. http://developer.android.com/reference/android/support/v4/view/PagerAdapter.html#instantiateItem(android.view.ViewGroup, int)
  2. ViewPager PagerAdapter not updating the View
Community
  • 1
  • 1
suman das
  • 5
  • 4

1 Answers1

0
  1. Create a condition where you parse your server response.
  2. if(imageUrl!=null){ // write code to add image url in your list, which will you pass in your adapter. } After using it, it will pass in adapter only those value in adapter which have some value, then it will work.

  3. You can place this condition in adapter when you pass your image-Url list and assign value in your adapter list. then before assigning your value please check same condition then assign.

Yogendra
  • 4,817
  • 1
  • 28
  • 21
  • Excellent dude... made three days effort look small... +1, but not able to accept because of some reputation thing... – suman das Apr 06 '15 at 05:02
  • One more thing, is there any way it can be done in the adapter class without sending the updated/modified response, then please suggest!! Thanks once again for your answer! – suman das Apr 06 '15 at 05:03