我正在创建一个记事本程序,用户可以在其中将图像添加到他的笔记中。当他创建新笔记并拾取一些图像时,它们的路径(uri)存储在arraylist中,该列表转换为xml标记(字符串类型转换)。当用户想要打开创建的注释时,从以前选择的图像会显示在gridview中。但是,当我尝试将字符串标记从xml文件转换为arraylist以便imageadapter可以将它们放入网格视图中时,出现转换类型错误。 edit 我得到的错误是-java.lang.String无法转换为android.net.Uri-
适配器是这个
`private Context ctx;
private int pos;
private LayoutInflater inflater;
private ImageView ivGallery;
ArrayList<Uri> mArrayUri;
public GalleryAdapter(Context ctx, ArrayList<Uri> mArrayUri) {
this.ctx = ctx;
this.mArrayUri = mArrayUri;
}
@Override
public int getCount() {
return mArrayUri.size();
}
@Override
public Object getItem(int position) {
return mArrayUri.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
pos = position;
inflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.gv_item, parent, false);
ivGallery = (ImageView) itemView.findViewById(R.id.ivGallery);
ivGallery.setImageURI(mArrayUri.get(position));
return itemView;
}`
而我尝试将它们转换为数组并在网格视图中显示的方式是
` String Paths = userData.get(1);
ArrayList mArrayUri = new ArrayList<>(Arrays.asList(Paths.split(",")));
galleryAdapter = new GalleryAdapter(getApplicationContext(),mArrayUri);
gvGallery.setAdapter(galleryAdapter);
gvGallery.setVerticalSpacing(gvGallery.getHorizontalSpacing());
ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) gvGallery
.getLayoutParams();
mlp.setMargins(0, gvGallery.getHorizontalSpacing(), 0, 0);
`
xml创建代码是这个
` xmlSerializer.startDocument("UTF-8", true);
xmlSerializer.startTag(null, "userData");
xmlSerializer.startTag(null,"Text");
xmlSerializer.text(NoteText);
xmlSerializer.endTag(null, "Text");
xmlSerializer.startTag(null,"Image Paths");
xmlSerializer.text(String.valueOf(Gallery.ImagePaths));
xmlSerializer.endTag(null,"Image Paths" );
xmlSerializer.endTag(null, "userData");
xmlSerializer.endDocument();`
答案 0 :(得分:0)
您的代码非常好,只需将数组列表类型设置为字符串,然后将字符串值设为uri。
在.text(String.valueof(Imagepath))
中使用Uri.toString
代替String valueof
示例:
Uri uri;
String myUriString;
myUriString = uri.toString();