将图像从活动传输到适配器类



这是我的活动A

case R.id.buttonSaveAdd:
        EditText editTextTitle = (EditText) findViewById(R.id.editTextTitle);
        EditText editTextDes = (EditText) findViewById(R.id.editTextDescription);
        EditText editTextOwner = (EditText) findViewById(R.id.editTextOwnerName);
        EditText editTextOwnerEmail = (EditText) findViewById(R.id.editTextOwnerEmail);
        EditText editTextPrice = (EditText) findViewById(R.id.editTextPrice);
        //populating data object from the values received 
        //from view
        String title = editTextTitle.getText().toString(); 
        String description = editTextDes.getText().toString();
        String ownerName = editTextOwner.getText().toString();
        String ownerEmail = editTextOwnerEmail.getText().toString();
        String pricce = editTextPrice.getText().toString();
        Advertisement  object = new Advertisement(title, description,
                ownerName, ownerEmail, "ic_launcher", Integer.parseInt(pricce), 100);
        Button_mak.ads.add(object);
        this.finish();
        break;
    }
}
   @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) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            ImageView imageView10 = (ImageView) findViewById(R.id.creatae);
            imageView10.setImageBitmap(BitmapFactory.decodeFile(picturePath));
            //// I want to send this image to my adapter class.

这是我的适配器类

public class MyAdapter extends ArrayAdapter<Advertisement> implements OnClickListener {
public static int totalBill = 0;
private final Context context;
private final Advertisement[] values;
public MyAdapter(Context context, Advertisement[] values) {
    super(context, R.layout.single_adds_layout, values);
    this.context = context;
    this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.single_adds_layout, parent, false);
    TextView title = (TextView) rowView.findViewById(R.id.title);
    TextView detailedMessage = (TextView) rowView.findViewById(R.id.detailed_message);
    TextView ownerName = (TextView) rowView.findViewById(R.id.owner);
    TextView ownerEmail = (TextView) rowView.findViewById(R.id.owner_email);
    TextView price = (TextView) rowView.findViewById(R.id.price);
    ImageView image = (ImageView) rowView.findViewById(R.id.imageView10);
    Button btn33 = (Button) rowView.findViewById(R.id.buybutton);
    btn33.setOnClickListener(this);
    Advertisement dataObj = values[position];
    title.setText(dataObj.getTitle());
    detailedMessage.setText(dataObj.getDetailedMessage());
    ownerName.setText(dataObj.getOwnerName());
    ownerEmail.setText(dataObj.getOwnerEmail());
    price.setText(String.valueOf(dataObj.getPrice()));
    btn33.setTag(price.getText());
    setDrawable(image, dataObj.getImageId());
    return rowView;
}
private void setDrawable(ImageView image, String drawableName) {
    AssetManager manager = image.getContext().getAssets();
    // Read a Bitmap from Assets
    InputStream open = null;
    try {
        open = manager.open(drawableName+".jpg");
        Bitmap bitmap = BitmapFactory.decodeStream(open);
        // Assign the bitmap to an ImageView in this layout
        image.setImageBitmap(bitmap);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (open != null) {
            try {
                open.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    } 
}
@Override
public void onClick(View v) {
    String value = (String) v.getTag();
    int valueInt = Integer.parseInt(value);
    MyAdapter.totalBill+= valueInt;

}

这里在第一个活动中,我从厨房加载图像。当我点击保存按钮在我的活动A我希望它发送图像,并得到它在我的适配器类

在Adapter中为位图创建一个setImage方法,在点击保存按钮时,调用Adapter。setImage方法并传递该图像并将其保存在适配器中。调用适配器的notifyDataSetChanged()来刷新数据集

最新更新