我正在尝试获取图像的ID以在完整视图中显示它

时间:2013-10-30 04:34:41

标签: java android gridview

下面是我无法获取图片ID资源的类。我试图从Web服务获取所有数据。在这里,我已经发布了从中获取图像的所有类。我将所有图像显示在gridView中。我没有遇到问题,但是当我点击gridview的图像时,它向我展示了我调用的xml文件。我想我在FullImageAcitivity文件中调用id的地方做错了。

DefaultGridView.java

 public class DefaultGridView  extends Activity {


//int position;

public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
finish();

}

GridView gridView;
Context context=this;
DisplayImageOptions options;
protected ImageLoader imageLoader = ImageLoader.getInstance();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.latestphotos);

        gridView=(GridView)findViewById(R.id.grid_view);

        options = new DisplayImageOptions.Builder()
        .showStubImage(R.drawable.stub_image)
        .showImageForEmptyUri(R.drawable.image_for_empty_url)
        .cacheInMemory()
        .cacheOnDisc()
        .bitmapConfig(Bitmap.Config.RGB_565)
        .build();


        gridView.setAdapter(new ImageAdapter());

    }

    public class ImageAdapter extends BaseAdapter {
        LayoutInflater inflater = LayoutInflater.from(context);



        public ImageAdapter() {
            // TODO Auto-generated constructor stub
            imageLoader.init(ImageLoaderConfiguration.createDefault(DefaultGridView.this));
        }
        @Override
        public int getCount() {
            return Global.getPhotos_list().size();
        }

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

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

        @Override
        public View getView(int position, View view, ViewGroup viewGroup) {
            View v = view;
            ImageView picture;
            TextView name;


            if(v == null) {
                v = inflater.inflate(R.layout.other, viewGroup, false);
                v.setTag(R.id.picture, v.findViewById(R.id.picture));
                v.setTag(R.id.text, v.findViewById(R.id.text));
            }

            picture = (ImageView)v.getTag(R.id.picture);
            name = (TextView)v.getTag(R.id.text);

            Item item = (Item)items.get(i);
            picture.setImageResource(item.getDrawable());
            name.setText(item.name);
            Log.d("position", position+"");

            imageLoader.displayImage(Constant.img_URL+Global.getPhotos_list().get(position).get("photo_name"), picture , options,null,context);
            gridView.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View v,
                        int position, long a) {




                      Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
                      i.putExtra("idkey", position); // pass the id
                      startActivity(i);

                }
            });
            return v;



        }
    }
}

FullImageActivity.java

public class FullImageActivity extends Activity {



Button download, setas;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.full_image);




    setas = (Button) findViewById(R.id.setas);




    download = (Button)findViewById(R.id.download);



 // get intent data
    final Intent i = getIntent();

    // Selected image id




    ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
    int id = getIntent().getIntExtra("idkey",-1); //get id
    imageView.setImageResource(id);

    setas.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
             WallpaperManager myWallpaperManager
             = WallpaperManager.getInstance(getApplicationContext());
            try {
                myWallpaperManager.setResource(R.id.full_image_view);
            } catch (Exception e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
            }
        }
    });
}

}

ImageAdapter.java

public class ImageAdapter extends BaseAdapter {

private LayoutInflater inflater;

List<Item> items;
public ImageAdapter(Context context,List<Item> items) {
 inflater = LayoutInflater.from(context);
 this.items = items;


}

@Override
public int getCount() {
    return items.size();
}

@Override
public Object getItem(int i) {
    return items.get(i);
}

@Override
public long getItemId(int i) {
    return items.get(i).drawable;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    View v = view;
    ImageView picture;
    TextView name;

    if(v == null) {
        v = inflater.inflate(R.layout.other, viewGroup, false);
        v.setTag(R.id.picture, v.findViewById(R.id.picture));
        v.setTag(R.id.text, v.findViewById(R.id.text));
    }

    picture = (ImageView)v.getTag(R.id.picture);
    name = (TextView)v.getTag(R.id.text);

    Item item = (Item)items.get(i);
    picture.setImageResource(item.getDrawable());
    name.setText(item.name);

    return v;
}

}

Items.java

public class Item {
String name;
int drawable;
 public int getDrawable() {
  return drawable;
}
public void setDrawable(int drawable) {
this.drawable = drawable;
}
public Item(String name, int id)
{
this.name= name;
this.drawable = id;
}
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

2 个答案:

答案 0 :(得分:1)

在gridView项目中单击

Item item =(Item) items.get(position);
int  id = item.getDrawable(); 
Intent i = new Intent(ActivityName.this, FullImageActivity.class);
i.putExtra("idkey", id); // pass the id
startActivity(i);

然后在FullImageActivity

int id = getIntent().getIntExtra("idkey"); //get id
imageview.setImageResource(id); // set the drawable to imageview

您可以将以下内容移至DefaultFridView的onCreate

  gridView.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View v,
                        int position, long a) {
                Item item =(Item) items.get(position);
                int  id = item.getDrawable(); 
                Intent i = new Intent(ActivityName.this, FullImageActivity.class);
                i.putExtra("idkey", id); // pass the id
                startActivity(i);

                }
            });

此外,您还需要在DefaultGridView中使用List

 List<Item> items = new ArrayList<Item>();              

然后

items.add(new Item("One", R.drawable.abstact_one));
items.add(new Item("Two",  R.drawable.abstract_three));
items.add(new Item("Three", R.drawable.image_two));
items.add(new Item("Four", R.drawable.image_four));
items.add(new Item("Five", R.drawable.image_five));
items.add(new Item("Six",  R.drawable.image_nine));
items.add(new Item("Seven", R.drawable.image_ten));

然后

 gridView.setAdapter(new ImageAdapter(),items);

另外,我不理解将ImageAdapter作为单独的内容和内部类。你为什么需要两个

答案 1 :(得分:0)

试试这个例子我是有用的

Grieview Display Full Image