I am using a Thread to perform an HttpURLConnection and get data from my database. The code below represents what I would like to accomplish but I get an error on the line
str_Data = "John Doe";
Error: Variable 'str_Data' is accessed from within inner class
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.MainActivity);
String str_Name = "";
str_Name = setDataToText(str_Url);
}
private String setDataToText(String urlStr) {
final String url = urlStr;
String str_Data = "";
new Thread() {
public void run() {
//A code to retrieve data is executed
//Data is Converted and added to the string str_Data;
str_Data = "John Doe";
}
}
return str_Data;
}
I would like to set the value of str_Data inside the run() operation on my new Thread() to the data that was recovered from my Database.
EDIT: THIS IS HOW I SOLVED THE PROBLEM, Let me know if it is not good practice when using this method, thanks for the help:
String str_Data = "";
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.MainActivity);
setDataToText(str_Url);
txtName.setText(str_Data);
}
private void setDataToText(String urlStr) {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
//A code to retrieve data is executed
//Data is Converted and added to the string str_Data;
str_Data = "John Doe";
}
}).start();
}