从Json加载Picasso中的图像

时间:2015-08-06 20:00:30

标签: android json gridview imageview picasso

您好我想使用Json在Picasso中加载图像库我该如何实现?我搜索了很多次,但是我找到的教程是使用本地图像或使用代码中的URL NOT JSON 请帮助!

这是我的代码但它使用本地图像:

MainActivity(图像加载 GridView

import android.app.Activity;
        import android.content.Context;
        import android.content.Intent;
        import android.os.Bundle;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.AdapterView;
        import android.widget.BaseAdapter;
        import android.widget.GridView;
        import android.widget.ImageView;

        import com.squareup.picasso.Picasso;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));

        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MainActivity.this, ActivityTwo.class);
                intent.putExtra("position", position);
                startActivity(intent);
            }
        });
    }

    //    our custom adapter
    private class ImageAdapter extends BaseAdapter {
        private Context mContext;

        public ImageAdapter(Context context) {
            mContext = context;
        }

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

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView,
                            ViewGroup parent) {
            ImageView imageView;
//            check to see if we have a view
            if (convertView == null) {
//                no view - so create a new one
                imageView = new ImageView(mContext);
            } else {
//                use the recycled view object
                imageView = (ImageView) convertView;
            }

//            Picasso.with(MainActivity.this).setDebugging(true);
            Picasso.with(MainActivity.this)
                    .load(mThumbIds[position])
                    .placeholder(R.raw.place_holder)
                    .error(R.raw.big_problem)
                    .noFade().resize(150, 150)
                    .centerCrop()
                    .into(imageView);
            return imageView;
        }
    }

    static Integer[] mThumbIds = {R.raw.amazed, R.raw.angelic,
            R.raw.cool, R.raw.crying, R.raw.devil,
            R.raw.laughing, R.raw.loving, R.raw.question,
            R.raw.sad, R.raw.silence, R.raw.simple, R.raw.sleeping,
            R.raw.smiling, R.raw.tongue, R.raw.winking, R.raw.worried,
            R.raw.amazed, R.raw.angelic, R.raw.cool, R.raw.crying,
            R.raw.devil, R.raw.laughing, R.raw.loving, R.raw.question,
            R.raw.sad, R.raw.silence, R.raw.simple, R.raw.sleeping,
            R.raw.smiling, R.raw.tongue, R.raw.winking, R.raw.worried};
}

单击图像 ActivityTwo 处理请求并在 ImageView中加载单击图像 这是代码:

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

public class ActivityTwo extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);

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

        int position = getIntent().getIntExtra("position", -1);
        if (position != -1) {
            Picasso.with(ActivityTwo.this)
                    .load(MainActivity.mThumbIds[position])
                    .placeholder(R.raw.place_holder)
                    .noFade()
                    .resize(800, 800)
                    .centerCrop()
                    .error(R.raw.big_problem)
                    .into(imageView);
        } else {
            Picasso.with(ActivityTwo.this)
                    .load(R.raw.big_problem)
                    .noFade()
                    .resize(800, 800)
                    .centerCrop()
                    .into(imageView);
        }
    }
}

我是android的新手请问如果你能写一个代码来解决这个问题告诉我应该把它放在哪里

1 个答案:

答案 0 :(得分:0)

现在就是你要做的事情:

phaseComment

你有一个jsonarray的json对象: 让我们说一下数组urlArray的名称:

phaseId

然后返回网址,这里有一个可以传递给适配器的字符串列表。 在适配器中,您将它们传递给Picasso