1

I am Working on a simple news app. I want to fetch JSON data and display it in my app. I have created a prototype in which text is defined as a string. I want to load these text and image from JSON. how I can do that.

My Adapter Code

public class VerticlePagerAdapter extends PagerAdapter {

    String mResources[] = {"Spider-Man is a fictional superhero appearing in American comic books published by Marvel Comics. The character was created by writer-editor Stan Lee and writer-artist Steve Ditko.",
    "Iron Man is a 2008 American superhero film based on the Marvel Comics character of the same name.\n" +
            "\n"};

    Context mContext;
    LayoutInflater mLayoutInflater;

    public VerticlePagerAdapter(Context context) {
        mContext = context;
        mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return 10;
    }

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View itemView = mLayoutInflater.inflate(R.layout.content_main, container, false);

        TextView label = (TextView) itemView.findViewById(R.id.textView);

        ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);

        if (position % 2 == 0) {
            label.setText(mResources[0]);
            imageView.setImageResource(R.drawable.images);
        }
        else{
            label.setText(mResources[1]);
            imageView.setImageResource(R.drawable.ironman);
        }

        container.addView(itemView);

        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((LinearLayout) object);
    }
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
nas
  • 29
  • 5
  • This will help you https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java – Raj Aug 28 '18 at 16:00
  • Possible duplicate of [How to parse JSON in Java](https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Jacob Celestine Aug 28 '18 at 16:39

0 Answers0