Why is setting a String in class not possible but getting the String possible?
Why is setting a String in class not possible but getting the String possible?
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:
Activity
RecyclerViewAdapter
Activity
RecyclerViewAdapter
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.
((Activity) context).startActivityForResult();
onActivityResult
intent
Code from Recyclerview .java
:
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).
Context
Activity
Code of onActivityResult
in Activty
:
onActivityResult
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.
getTesturl
RecyclerViewAdapter
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".
updateDataAdapter.java
public String testurl;
public String testurl = "This is my text";
getTesturl
setTesturl
So I tried to replace updateDataAdapter.setTesturl(String.ValueOf(uri2));
with updateDataAdapter.setTesturl("This is my text");
updateDataAdapter.setTesturl(String.ValueOf(uri2));
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.
2 Answers
2
'updateDataAdapter' should declared class member variable.
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();
}
Here you create a new updateDataAdapter
. Replace the newly created updateDataAdapter
with the existing RecyclerViewAdapter
.
updateDataAdapter
updateDataAdapter
RecyclerViewAdapter
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Comments
Post a Comment