如何在Intent中选择图像时在自定义列表视图中添加图像

时间:2013-06-26 03:50:11

标签: android listview uri photo

我想在每次选择意图选择器中的图像时将图像放入我的自定义列表视图中。 到目前为止我到了这里......

    public class ItemListBaseAdapter extends BaseAdapter {
private static ArrayList<ItemDetails> itemDetailsrrayList;
private static ArrayList<Uri> imagesUri;



private LayoutInflater l_Inflater;

   public ItemListBaseAdapter(Context context, ArrayList<ItemDetails> results,  ArrayList<Uri> uri) {
    itemDetailsrrayList = results;
    imagesUri = uri;
    l_Inflater = LayoutInflater.from(context);

}



public int getCount() {
    return itemDetailsrrayList.size();
}

public Object getItem(int position) {
    return itemDetailsrrayList.get(position);

}

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

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = l_Inflater.inflate(R.layout.item_details_view, null);
        holder = new ViewHolder();
        holder.txt_itemName = (TextView) convertView.findViewById(R.id.name);
        holder.txt_itemDescription = (TextView) convertView.findViewById(R.id.itemDescription);
        holder.txt_itemPrice = (TextView) convertView.findViewById(R.id.price);
        holder.itemImage = (ImageView) convertView.findViewById(R.id.photo);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    ArrayList<Uri> lastimageUri = itemDetailsrrayList.get(position).getUriImage();

    itemDetailsrrayList.get(position).setUriImage(lastimageUri.get(lastimageUri.size() - 1));
    holder.itemImage.setImageURI(itemDetailsrrayList.get(position).getImage());




    return convertView;
}

这是我的活动可以帮助我我认为我在arraylist中添加的Uri已被复制

public class ListViewImagesActivity extends Activity {
Button btn_GetPhotos;
private String selectedImagePath;
public Uri imageUri;
ArrayList<Uri> imageUris = new ArrayList<Uri>();
ArrayList<ItemDetails> results = new ArrayList<ItemDetails>();



ItemDetails item_details = new ItemDetails();
private final static int SELECT_PICTURE = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btn_GetPhotos = (Button)findViewById(R.id.btn_GetPhotos);
    btn_GetPhotos.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            startActivityForResult(Intent.createChooser(intent, "Select Photos"), SELECT_PICTURE);


        }
    });


}


private ArrayList<ItemDetails> GetSearchResults(){


    item_details.setUriImage(ImageListUri());
    results.add(item_details);




    return results;
}

public ArrayList<Uri> ImageListUri(){

    imageUris.add(imageUri);
    return imageUris;

}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE ) {
            imageUri = data.getData();
            ArrayList<ItemDetails> image_details = GetSearchResults();

            //ArrayList<Uri> resultImageUri = ImageListUri();

            ItemListBaseAdapter uriItem = new ItemListBaseAdapter(ListViewImagesActivity.this, image_details);
            final ListView lv1 = (ListView) findViewById(R.id.listV_main);
            lv1.setAdapter(uriItem);


        }
    }
}

}

但每次我选择图像时,它都会显示当前图像并替换我选择的上一张图像。因此,当我选择图片4x时,最新选择的图像会出现在我的列表视图4x中。我想显示我选择的每个图像,而不仅仅是最新的图像。

1 个答案:

答案 0 :(得分:0)

我认为问题是由

引起的

item_details.setUriImage(ImageListUri()); results.add(item_details);

函数GetSearchResults()中的

每次调用GetSearchResults()时,item_details都会更改为上次选择的图片。请注意,results.add(item_details);会添加item_details的引用,而不是item_details的副本。