20

I am create view pager with custom view pager adapter but when i run the project i can not see anything

  public class CustomAdapter extends PagerAdapter {

        private List<NewsObject> newsObjects;
        private Activity mActivity;
        private LayoutInflater inflater;
        private ImageLoaderConfiguration imageConfig;
        private ImageLoader imageLoader = ImageLoader.getInstance();

        public CustomAdapter(List<NewsObject> newsObjects, Activity mActivity) {
            super();
            Log.d("Con :", "structor");
            this.newsObjects = newsObjects;
            this.mActivity = mActivity;
            imageConfig = new ImageLoaderConfiguration.Builder(
                    mActivity.getApplicationContext()).build();
            imageLoader.init(imageConfig);
        }

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

        @Override
        public void destroyItem(View collection, int position, Object view) {
            ((ViewPager) collection).removeView((View) view);
        }

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

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            ImageViewPlus img_news_image;
            TextView txt_news_title;
            TextView txt_news_description;

            inflater = (LayoutInflater) mActivity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View viewLayout = inflater.inflate(R.layout.detail_news_page, null);

            img_news_image = (ImageViewPlus) viewLayout
                    .findViewById(R.id.img_detail_news_image);
            txt_news_title = (TextView) viewLayout
                    .findViewById(R.id.txt_detail_news_title);
            txt_news_description = (TextView) viewLayout
                    .findViewById(R.id.txt_detail_news_description);

            Log.d("TAG :", "instantiateItem");

            imageLoader.displayImage(newsObjects.get(position).getNews_image(),
                    img_news_image);

            Toast.makeText(mActivity.getApplicationContext(),
                    Constantz.newsObjects.get(position).getNews_id() + "",
                    Toast.LENGTH_SHORT).show();

            txt_news_title.setText(newsObjects.get(position).getNews_title());
            txt_news_description.setText(newsObjects.get(position)
                    .getNews_description());

            return viewLayout;
        }

    }

but the toast is displaying but i can not see any text on my application

vipul mittal
  • 16,683
  • 3
  • 38
  • 43
Kamal Upasena
  • 1,259
  • 3
  • 13
  • 38

4 Answers4

71

write

container.addView(viewLayout);

before returning the view in instantiateItem

  public Object instantiateItem(ViewGroup container, int position) {

        ImageViewPlus img_news_image;
        TextView txt_news_title;
        TextView txt_news_description;

        inflater = (LayoutInflater) mActivity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View viewLayout = inflater.inflate(R.layout.detail_news_page, null);

        img_news_image = (ImageViewPlus) viewLayout
                .findViewById(R.id.img_detail_news_image);
        txt_news_title = (TextView) viewLayout
                .findViewById(R.id.txt_detail_news_title);
        txt_news_description = (TextView) viewLayout
                .findViewById(R.id.txt_detail_news_description);

        Log.d("TAG :", "instantiateItem");

        imageLoader.displayImage(newsObjects.get(position).getNews_image(),
                img_news_image);

        Toast.makeText(mActivity.getApplicationContext(),
                Constantz.newsObjects.get(position).getNews_id() + "",
                Toast.LENGTH_SHORT).show();

        txt_news_title.setText(newsObjects.get(position).getNews_title());
        txt_news_description.setText(newsObjects.get(position)
                .getNews_description());
        container.addView(viewLayout);

        return viewLayout;
    }
vipul mittal
  • 16,683
  • 3
  • 38
  • 43
10

You have missed to add your view to the container. Need to add before return your view.

((ViewPager) container).addView(viewLayout);

also sure that you have set current item for View Pager.

Piyush
  • 23,959
  • 6
  • 36
  • 71
4

You aren't adding your view to the container.

container.addView(viewLayout, 0);
Jonathon Fry
  • 2,902
  • 3
  • 21
  • 30
-1
<TabHost
     ......
<TabWidget
.....
<FrameLayout
     android:id="@android:id/tabcontent"
     android:layout_width="0dp"
     android:layout_height="0dp" >
</FrameLayout>
<android.support.v4.view.ViewPager
  • 12
    Some explanation wouldn't hurt. See [How do I write a good answer?](http://stackoverflow.com/help/how-to-answer) – jub0bs Sep 28 '14 at 18:11