1

下面是我无法获取图像 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;
}

}

项目.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;
}

}
4

2 回答 2

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<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 作为单独的内部类。为什么你需要两者

于 2013-10-30T04:44:35.253 回答
0

试试这个例子,我觉得它很有用

Grieview 显示完整图像

于 2013-10-30T05:03:48.107 回答