如何在另一个活动的列表视图中显示所选图像。从URL获取图像

时间:2016-01-27 11:06:23

标签: android listview picasso

Image Adapter Class

public class ImageListAdapter extends ArrayAdapter {
private Context context;
private LayoutInflater inflater;

private String[] imageUrls;

public ImageListAdapter(Context context, String[] imageUrls) {
    super(context, R.layout.listview_item_image, imageUrls);

    this.context = context;
    this.imageUrls = imageUrls;

    inflater = LayoutInflater.from(context);
 }
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (null == convertView) {
        convertView = inflater.inflate(R.layout.listview_item_image, parent, false);
    }
    Picasso
            .with(context)
            .load(imageUrls[position])
            .fit() // will explain later
            .into((ImageView) convertView);

    return convertView;

主类

 public class UsageExampleAdapterextends AppCompatActivity {
public static String[] eatFoodyImages = {

        "http://i.imgur.com/76Jfv9b.jpg",
        "http://i.imgur.com/fUX7EIB.jpg",
        "http://i.imgur.com/syELajx.jpg",
        "http://i.imgur.com/COzBnru.jpg",
        "http://i.imgur.com/Z3QjilA.jpg",
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_usage_example_adapter);
    ListView listView = (ListView) findViewById(R.id.listView);
    //  listView.setAdapter(adapter);
    listView.setAdapter(new ImageListAdapter(UsageExampleAdapter.this, eatFoodyImages));

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            Intent intent = new Intent(UsageExampleAdapter.this, SingleItemView.class);
            intent.putExtra("position", position);
            startActivity(intent);


        }

    });
}
 }

SingleItemView类

    public class SingleItemView extends Activity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.singleitemview);
    ImageView imageView = (ImageView) findViewById(R.id.imageView);
  int  position = getIntent().getIntExtra("position", -1);
    if(position !=1){
        Picasso.with(this)
                .load(UsageExampleAdapter.eatFoodyImages[position])
                .into(imageView);
    }
   }

我从网址获取图片并使用毕加索我在listview中显示它。从listview中选择图像我想在另一个活动中显示它。在做上述事情时,我得到一个空白的白色活动。

2 个答案:

答案 0 :(得分:0)

替换此代码

 if(position !=1){
        Picasso.with(this)
                .load(UsageExampleAdapter.eatFoodyImages[position])
                .into(imageView);
    }

if(position !=1){
            Picasso.with(this)
                    .load(Main.eatFoodyImages[position])
                    .into(imageView);
        }

答案 1 :(得分:0)

不应该,

if(position != -1){
    Picasso.with(this)
            .load(UsageExampleAdapter.eatFoodyImages[position])
            .into(imageView);
}