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);
}
}