-1

I'm working on a short news application. I have developed a prototype for it which uses data which is defined. I want to fetch this data from a JSON. How can I do that? I'm a beginner at Android programming. If possible, please provide the code/syntax that I can use to be able to fetch the data I need.

My adapter Code:

public class VerticlePagerAdapter extends PagerAdapter {

    Context context;
    LayoutInflater inflater;

    // list of images
    public int[] lst_images = {
            R.drawable.image_1,
            R.drawable.image_2,
            R.drawable.image_3,
            R.drawable.image_4
    };
    // list of titles
    public String[] lst_title = {
            "COSMONAUT",
            "SATELITE",
            "GALAXY",
            "ROCKET"
    }   ;
    // list of descriptions
    public String[] lst_description = {
            "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,",
            "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,",
            "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,",
            "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,"
    };
    // list of background colors
    public int[]  lst_backgroundcolor = {
            Color.rgb(55,55,55),
            Color.rgb(239,85,85),
            Color.rgb(110,49,89),
            Color.rgb(1,188,212)
    };


    public VerticlePagerAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return lst_title.length;
    }

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.content_main,container,false);
        LinearLayout layoutslide = (LinearLayout) view.findViewById(R.id.slidelinearlayout);
        ImageView imgslide = (ImageView)  view.findViewById(R.id.imageView);
        TextView txttitle= (TextView) view.findViewById(R.id.textView_link);
        TextView description = (TextView) view.findViewById(R.id.textView);
        layoutslide.setBackgroundColor(lst_backgroundcolor[position]);
        imgslide.setImageResource(lst_images[position]);
        txttitle.setText(lst_title[position]);
        description.setText(lst_description[position]);
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((LinearLayout)object);
    }
}
Luuklag
  • 3,897
  • 11
  • 38
  • 57
nas
  • 29
  • 5
  • Possible duplicate of [How To fetch data from json for slider](https://stackoverflow.com/questions/52052605/how-to-fetch-data-from-json-for-slider) – Taier Aug 30 '18 at 09:00
  • One Thing to improve may be to stop Posting the same Question again and again. Show Research effort and stop asking for "Please give me the correct code for feching these data". – ProgFroz Aug 30 '18 at 09:14

1 Answers1

0

first, you will need to use an api response and you may use https://www.mockapi.io or https://www.getpostman.com/ or any other api providers to generate your response second you can use retrofit library to handle network connection and get your response https://square.github.io/retrofit/

Hazem Ashraf
  • 310
  • 2
  • 8