【问题标题】:Adding array of Uris into a GridView将 Uris 数组添加到 GridView
【发布时间】:2015-11-21 13:03:44
【问题描述】:

在gridView中设置Uris的ArrayList时,只显示一项。为什么?

适配器:

public class ImageGridAdapter extends BaseAdapter {

private Context mContext;

public ImageGridAdapter(Context c) {

    this.mContext = c;
}

@Override
public int getCount() {
    try {
        return PictureGroupActivity.ALofSelectedImgs.size();
    } catch (NullPointerException e) {
        return 0;
    }
}

@Override
public Object getItem(int position) {

    return PictureGroupActivity.ALofSelectedImgs.get(position);
}

@Override
public long getItemId(int position) {

    return position;
}


public View getView(int position, View convertView, ViewGroup parent) {


    ImageView imageView;
    if (convertView == null) {
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(5, 5, 5, 5);
    }  else {
        imageView = (ImageView) convertView;
    }

    try {
        imageView.setImageURI(PictureGroupActivity.ALofSelectedImgs.get(position));
        Toast.makeText(mContext.getApplicationContext(), "Idee: " + PictureGroupActivity.ALofSelectedImgs, Toast.LENGTH_SHORT).show();
    }catch (NullPointerException e) {}

    return imageView;
}

设置适配器:

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

    GridView gridView = (GridView) findViewById(R.id.picture_group_gridView);
    gridView.setAdapter(new ImageGridAdapter(this));
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(PictureGroupActivity.this, "You clicked " + position, Toast.LENGTH_SHORT).show();
        }
    });
}

从我拍摄图像的位置(从手机图库中选择后):

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        selectedImage = data.getData();

            ALofSelectedImgs = new ArrayList<Uri>();
            ALofSelectedImgs.add(selectedImage);


        Intent restart = getIntent();
        finish();
        startActivity(restart);

    }
}

如何使它将多个图像添加到 ArrayList 中并使其保持在那里并且不会相互覆盖?

【问题讨论】:

  • 那个 ArrayList 是静态的吗?每次从图库中选择图像时,您似乎都在重新创建它
  • 它是静态的,否则我无法将它带到适配器,或者我会?

标签: android gridview arraylist imageview adapter


【解决方案1】:

我可以在这里看到您的方法存在一些问题。

最简单的解决方案(更改最少的代码)。将ALofSelectedImgs = new ArrayList();onActivityResult(int, int, Intent)移到onCreate(Bundle)

这仍然不会在方向更改或关闭应用程序之间保留数据。每次调用 onCreate(Bundle) 时,您都会得到一个新的空 ArrayList。

我不建议使用这样的静态字段。对于初学者,您不能将 ImageGridAdapter 与任何其他 Activity 或 Fragment 一起使用。您需要在构造函数或 setter 方法中将 List 传递给它。这样您就可以更轻松地重复使用它。

private Context mContext;
private List<Uri> mUris;

public ImageGridAdapter(Context context, List<Uri> uris) {
    super(context);
    mContext = context;
    mUris = uris;
}

为了更好的解决方案...有很多方法可以做到这一点。这是我要做的:

  • 创建一个扩展 SQLiteOpenHelper 的类,以便将选定的 Uris 保存在 SQLite 表中。我从这个 tutorial 学到了很多东西。对于这么简单的事情,我可能不会担心 ContentProvider。如果你有兴趣,我可以给你一个模板,我用它来在一个合约类中保存许多表的列和其他常量。
  • 创建一个扩展 SimpleCursorAdapter 的适配器,以从包含 SQLite 表查询的游标创建视图。上述教程中对此进行了说明。
  • 在 PhotoGroupActivity 中,查询您的表并使用 onCreate(Bundle) 返回的光标初始化适配器。
  • onActivityResult(int, int, Intent) 中,您需要将 Uri 插入 SQLite 表,然后执行另一个查询并为适配器提供新的光标。

编辑: 回答你的第二个问题。起初我没有想到这一点,但您会希望使用缩略图在您的 GridView 中显示。使用Bitmap.createScaledBitmap(Bitmap, int, int, boolean) 创建缩略图。将缩略图存储在您的应用程序的私有存储中以避免它进入您的图库,然后将 Uri 添加到您的 ArrayList 的缩略图中。您可能希望跟踪全尺寸图像的 Uri 以及用户触摸缩略图的时间。

尝试使用 HashMap,缩略图 Uri 作为键,全尺寸 Uri 作为值。

// Create the HashMap like this:
HashMap<Uri, Uri> uriMap = new HashMap<>();

// You have the main Uri. Get the bitmap, create a thumbnail and store it.
// Add an entry to the HashMap like this:
uriMap.put(thumbnailUri, fullSizeUri);

// To get the list of thumbnail Uris for the adapter:
List<Uri> thumbnailList = new ArrayList<>(uriMap.keySet());

// When user presses an image in GridView, get the relevant full-size
// Uri like this:
fullSizeUri = uriMap.get(thumbnailUri);

再次编辑: 我再次查看并意识到我建议了一个 CursorAdapter,然后提供了有关如何获取数据的 HashMap 的信息。

相反,只需向 SQLite 表添加另一列并将两个 Uris 都存储在那里。获取 Uri 和缩略图 Uri 后,将它们存储在表中并在表中查询 SimpleCursorAdapter。

【讨论】:

  • 谢谢!它成功了。明天我有更多时间时会检查教程链接。 ^^ 但是现在,我还想问一件事。添加手机摄像头拍摄的图像时,应用程序变得非常缓慢(对 gridView 的任何操作,例如滚动甚至在返回 Activity 时重新加载都会导致 CPU 使用率上升)。当加载一堆这些图像时,应用程序崩溃了;但是当加载从网上下载的小图像时,即使它没有崩溃,它也运行顺利,有什么解决这个问题的建议吗?
  • 刚刚记得我有一个这样的例子,未完成但工作。这是一项任务。主要区别在于它使用相机拍摄图像,并为位图存储字符串路径而不是 URI。
  • 检查 DatabaseHelper.java 和 PhotoAdapter.java [链接] (github.com/fletcherjohns/DailyDuckFace)
  • 已经成功了,但非常感谢您的帮助! :)
猜你喜欢
  • 2016-11-07
  • 1970-01-01
  • 2011-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多