从Json获取数据并以字符串形式显示

时间:2018-08-28 15:54:15

标签: android android-json

我正在开发一个简单的新闻应用程序。我想获取JSON数据并将其显示在我的应用中。我创建了一个原型,其中将文本定义为字符串。我想从JSON加载这些文本和图像。我该怎么做。

我的适配器代码

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

0 个答案:

没有答案