如何将文件图像从onActivityResult带到适配器Android?

时间:2017-03-30 08:44:15

标签: android upload android-recyclerview onactivityresult

我有一个循环查看android并且每个项目中都有一个图像。在那个图像中我想制作一个上传功能。我在这个应用程序中使用MVP。所以,有片段,演示者和适配器。

我的片段

  public void loadList() {
    rv.setAdapter(new PendingOrderAdapter(transactionHistories, getContext(), presenter));
}

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

    switch (requestCode) {
        case 0:
            if (resultCode == RESULT_OK) {

                Uri selectedImage = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                Cursor cursor = getActivity().getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();
                file = new File(picturePath);
            }
    }
}

我的适配器

public class PendingOrderAdapter extends RecyclerView.Adapter<PendingOrderAdapter.PendingOrderHolder>  {

ArrayList<TransactionHistory> list;

Context context;
File file;
PendingOrderPresenter presenter;


public PendingOrderAdapter(ArrayList<TransactionHistory> list, Context context, PendingOrderPresenter presenter) {
    this.list = list;
    this.context = context;
    this.presenter = presenter;
}


@Override
public PendingOrderAdapter.PendingOrderHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_pending_order, parent, false);
    return new PendingOrderAdapter.PendingOrderHolder(itemView);
}

@Override
public void onBindViewHolder(PendingOrderAdapter.PendingOrderHolder holder, int position) {
    final TransactionHistory item = list.get(position);

    holder.txvPackageName.setText(item.getPackageName());
    holder.txvTransactionType.setText(item.getTransactionType());
    holder.txvOrderNumber.setText(item.getOrderNumber());
    holder.txvInvestmentNumber.setText(item.getInvestmentNumber());
    holder.txvTotal.setText(AmountFormatter.format(item.getAmount()));
    holder.txvTransactionStatus.setText(item.getTransactionStatus());

    SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.INVISEE_RETURN_FORMAT2,    Locale.getDefault());
    Date dateParse = null;
    try {
        dateParse = sdf.parse(item.getTransactionDate());
    } catch (ParseException e) {
        e.printStackTrace();
    }
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm" , Locale.getDefault());
    String convertedDate= formatter.format(dateParse);
    holder.txvDate.setText(convertedDate);

    holder.imgOrder.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            Activity origin = (Activity) context;
            origin.startActivityForResult(i, 0);
        }
    });

    presenter.uploadTransaction(item.getOrderNumber(), file);
}

@Override
public int getItemCount() {
    return list != null ? list.size() : 0;
}

public static class PendingOrderHolder extends RecyclerView.ViewHolder {

    @Bind(R.id.txvDate)
    TextView txvDate;
    @Bind(R.id.txvTransactionType)
    TextView txvTransactionType;
    @Bind(R.id.txvPackageName)
    TextView txvPackageName;
    @Bind(R.id.txvOrderNumber)
    TextView txvOrderNumber;
    @Bind(R.id.txvInvestmentNumber)
    TextView txvInvestmentNumber;
    @Bind(R.id.txvTotal)
    TextView txvTotal;
    @Bind(R.id.txvTransactionStatus)
    TextView txvTransactionStatus;
    @Bind(R.id.imgOrder)
    ImageView imgOrder;

    public PendingOrderHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }
}

问题是如何将图像文件带到我的适配器?

0 个答案:

没有答案