如何从图库意图使用接口在适配器bindonview中设置imageview?

时间:2018-07-09 06:20:21

标签: android recycler-adapter

我正在尝试通过在回收器视图中单击卡视图的onclicking imageview打开画廊,并且我已经从单击imageview来打开画廊,但是从图库图像视图中选择图像永远不会改变。 我有一个适配器类,在其中我使用接口来调用画廊意图以从活动中打开画廊,正在打开画廊,但是在选择图像时,图像视图永远不会改变,并且我不确定是什么问题还是问题,因为没有错误发生。

这是适配器类

public class Cheque_Adapter extends
RecyclerView.Adapter<Cheque_Adapter.ViewHolder> {

public ArrayList<Cheque_Payment> cheques_list;
public static  final int CA_REQUEST = 1;
public static final int IMG_REQUEST = 2;
private LayoutInflater inflater;
private OnImageClickChanged onImageClickChanged;
private Context contxt;
private static  final int REQUEST_IMAGE_CAPTURE =666;
private  static final int RESULT_LOAD_IMAGE =777;
private static final int CAMERA_REQUEST = 101;
Uri imageUri;
ImageView imageView;
String imageurl;


public interface OnImageClickChanged {

       String getPosition(String imageurl);
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    EditText Chq_Amount,Chq_No,Chq_Date;

    private ImageView imageView;

    public ViewHolder (View itemView) {
        super(itemView);

        Chq_Amount = (EditText) itemView.findViewById(R.id.chequeamount);
        Chq_No = (EditText) itemView.findViewById(R.id.chequenumber);
        Chq_Date =(EditText) itemView.findViewById(R.id.chequedate);
       imageView = (ImageView)itemView.findViewById(R.id.chequeimage);


        Chq_Amount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                 Cheque_Payment chqadd_list = cheques_list.get(getAdapterPosition());
                chqadd_list.setChequeAmount(charSequence+"");
                cheques_list.set(getAdapterPosition(),chqadd_list);

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        Chq_No.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                Cheque_Payment chqadd_list = cheques_list.get(getAdapterPosition());
                chqadd_list.setChequeNumber(charSequence+"");
                cheques_list.set(getAdapterPosition(),chqadd_list);
            }
            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        Chq_Date.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                Cheque_Payment chqadd_list = cheques_list.get(getAdapterPosition());
                chqadd_list.setChequeDate(charSequence+"");
                cheques_list.set(getAdapterPosition(),chqadd_list);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

    }

    @Override
    public void onClick(View view) {
        int position =getAdapterPosition();

    }
}

public Cheque_Adapter( Context ctx,ArrayList<Cheque_Payment> cheques_list)
{
    contxt =ctx;
    inflater = LayoutInflater.from(ctx);
    this.cheques_list = cheques_list;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View view = inflater.inflate(R.layout.payment_layout, parent, false);
     return new ViewHolder(view);
}

public void setOnImageClickChanged(OnImageClickChanged onImageClickChanged)
{
    this.onImageClickChanged =onImageClickChanged;

}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {

    Cheque_Payment chqadd_list = cheques_list.get(position);

    final String data = cheques_list.get(position).toString();

    holder.imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

             onImageClickChanged.getPosition(imageurl);


                    Toast.makeText(view.getContext(), "the message : "+imageurl,
                    Toast.LENGTH_SHORT).show();

        }
    });
    holder.Chq_Amount.setText(cheques_list.get(position).getChequeAmount());
    holder.Chq_No.setText(cheques_list.get(position).getChequeNumber());
    holder.Chq_Date.setText(cheques_list.get(position).getChequeDate());
}

@Override
public int getItemCount() {
    return cheques_list.size();

}

public ArrayList<Cheque_Payment> getArrayList()
{
    return cheques_list;
}

}

这是活动课程

public class Recieve_Payment extends AppCompatActivity implements Cheque_Adapter.OnImageClickChanged {

在此类中使用了selectimage方法来打开图库

private void selectimage()
{

    Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent, 2);



}


@Override

public String getPosition(String imageurl) {
    selectimage();

    getimagex = imageurl;
    return getimagex;
}

}

我认为onactivityresult永远不会被正确使用

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == IMG_REQUEST && resultCode == RESULT_OK && data!= null )
    {
        Uri path = data.getData();
        getimagex =path.toString();
          Toast.makeText(this,"path" + path.toString() ,Toast.LENGTH_SHORT).show();
        try {

            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),path);
            imageview.setImageBitmap(bitmap);


        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

如何从此处将图像设置为回收者视图中的图像视图

0 个答案:

没有答案