【问题标题】:Display image based on Gridview selection from previous activity根据从先前活动中选择的 Gridview 显示图像
【发布时间】:2011-07-19 07:25:13
【问题描述】:

我目前有一个显示一些缩略图的有效网格视图。目前,当您选择一张图片时,它会将更大分辨率的图片保存为壁纸。

我要做的是打开一个新意图,然后根据单击的 GridView 位置显示全分辨率图像。我不确定如何从新活动/意图中找出点击了哪个位置。

这就是我的主要活动

public class test extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

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

    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(test.this, "" + position + "" + id, Toast.LENGTH_SHORT).show();

            //Make a Bitmap from the Resource
            ImageAdapter i = (ImageAdapter)parent.getAdapter();
            Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),(int)i.getItemId(position));

            //Get the WallpaperManager
            WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());

            try {
                //Set the clicked bitmap
                myWallpaperManager.setBitmap(mBitmap);
                Toast.makeText(test.this, "Wallpaper set", Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                Toast.makeText(test.this, "Error setting wallpaper", Toast.LENGTH_SHORT).show();
            }

        }
    });

}

}

图像适配器

public class ImageAdapter extends BaseAdapter {

private Context mContext;

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

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

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

public long getItemId(int position) {
    return mFullSizeIds[position];
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

// references to our images
private Integer[] mThumbIds = {
        R.drawable.sample_1, R.drawable.sample_2,
        R.drawable.sample_3, R.drawable.sample_4,
        R.drawable.sample_5, R.drawable.sample_6,
        R.drawable.sample_7 
};


private Integer[] mFullSizeIds = {
        R.drawable.wallpaper1,
        R.drawable.wallpaper2,
        R.drawable.wallpaper3,
        R.drawable.wallpaper4,
        R.drawable.wallpaper5,
        R.drawable.wallpaper6,
        R.drawable.wallpaper7
        };

}

和空白的 FullView 模板

public class FullView extends Activity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fullview);

ImageView imageview;
    imageview.setImageResource(mFullSizeIds[**]) <--- How to set ** to what GridView position was selected?


}

}

由于不知道如何在这个新活动中传递从 GridView 中单击的位置,我还没有真正在 FullView 活动中编写任何内容。

(我也没有在我的主要活动中编写意图代码,所以你可以看到它目前是如何工作的)

如何将选中的 gridview 位置传递到新活动中?

【问题讨论】:

    标签: android gridview android-intent imageview


    【解决方案1】:

    在您的事件处理程序(OnItemClickListener)中,您可以获得图像的资源 ID 作为 mThumbs[position]。将此作为额外内容添加到您用于启动 FullView Activity 的 Intent 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-08
      • 1970-01-01
      • 2013-10-14
      • 1970-01-01
      • 1970-01-01
      • 2021-02-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多