My problem includes an Activity
and a RecyclerViewAdapter
.
I want to pass the Stringvalue of an Uri from my Activity
to my RecyclerViewAdapter
.
For passing I am using this Getter and Setter class:
public class updateDataAdapter {
public String testurl;
public String getTesturl() {
return testurl;
}
public void setTesturl(String testurl) {
this.testurl = testurl;
}
}
The Recyclerview sends an intent with ((Activity) context).startActivityForResult();
and the Activity uses onActivityResult
to receive the intent
and RequestCode.
Code from Recyclerview .java
:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
((Activity) context).startActivityForResult(Intent.createChooser(intent, "Select Image From Gallery"), 1);
The Context
is the Activity
(Given as parameter at Setting the adapter).
Code of onActivityResult
in Activty
:
@Override
public void onActivityResult(int RC, int RQC, Intent I) {
super.onActivityResult(RC, RQC, I);
if (RC == 1 && RQC == RESULT_OK && I != null && I.getData() != null) {
Uri uri2 = I.getData();
updateDataAdapter updateDataAdapter = new updateDataAdapter();
try {
updateDataAdapter.setTesturl(String.ValueOf(uri2));
} catch(IOException e) {
e.printStackTrace();
}
}
}
If I call getTesturl
in my RecyclerViewAdapter
the String is empty.
That's why I tried different things to locate the problem.
First I set (in my updateDataAdapter.java
) public String testurl;
to public String testurl = "This is my text";
and calling getTesturl
receives the text "This is my text".
setTesturl
.So I tried to replace updateDataAdapter.setTesturl(String.ValueOf(uri2));
with updateDataAdapter.setTesturl("This is my text");
But in this case the String is empty.
How can I solve the problem and why is my code not working?
Please help me.
'updateDataAdapter' should declared class member variable.
private updateDataAdapter updateDataAdapter = new updateDataAdapter(); @Override public void onActivityResult(int RC, int RQC, Intent I) { super.onActivityResult(RC, RQC, I); if (RC == 1 && RQC == RESULT_OK && I != null && I.getData() != null) { Uri uri2 = I.getData(); try { updateDataAdapter.setTesturl(String.ValueOf(uri2)); } catch(IOException e){ e.printStackTrace(); }} }