我是Stackoverflow的新手。
我正在尝试创建一个单位转换器,其中函数“calculateCurrency”返回结果的Double值。但TextView“outputNumber”的值总是根据以前的输入而改变。
Ex-如果我输入1,那么outputNumber将更改为“null”但是当我在1之后输入另一个数字时,outputNumber将更改为它应该在1上更改的值。但是Toast正在显示正确值,即1,它显示1,因此myInputNumber的值根据输入正确。
任何人都可以帮助我吗?
inputNumber.addTextChangedListener(
new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
try {
myInputNumber = Double.parseDouble(s.toString());
Double myText = calculations.calculateCurrency(myInputNumber, convertFrom, convertTo);
Toast.makeText(MainActivity.this, myInputNumber+"", Toast.LENGTH_SHORT).show();
outputNumber.setText(myText+"");
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
}
}
);
calculate.calculateCurrency的代码是:
public Double calculateCurrency(Double input, String convertFrom, String convertTo) throws ExecutionException, InterruptedException {
myCurrencyFetcher fetchCurrency = new myCurrencyFetcher();
fetchCurrency.execute(Double.toString(input), convertFrom, convertTo);
return myText;
}
class myCurrencyFetcher extends AsyncTask<String, Double, Double>{
URL url;
StringBuffer stringBuffer;
@Override
protected Double doInBackground(String... params) {
try{
Double myInputNumber = Double.parseDouble(params[0]);
String convertFrom = params[1];
String convertTo = params[2];
url = new URL("http://api.fixer.io/latest?base="+convertFrom+"&symbols="+convertTo);
HttpURLConnection connection;
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
stringBuffer = new StringBuffer();
String line = "";
while((line = reader.readLine()) != null){
stringBuffer.append(line);
}
String finalJSON = stringBuffer.toString();
JSONObject parentObject = new JSONObject(finalJSON);
JSONObject finalObject = parentObject.getJSONObject("rates");
String rate = finalObject.getString(convertTo);
//String year = finalObject.getString("year");
Double myRate = Double.parseDouble(rate);
return myInputNumber*myRate;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Double result) {
super.onPostExecute(result);
Calculations.myText = result;
}
}
答案 0 :(得分:0)
再次尝试在方法calculations.calculateCurrency(myInputNumber, convertFrom, convertTo);
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?android:selectableItemBackground">
<ImageButton
android:id="@+id/one_button"
android:background="@drawable/ripple_animation"
android:padding="0dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:layout_width="85dp"
android:layout_height="85dp"
app:srcCompat="@drawable/one_button_500_500"
android:layout_below="@+id/four_button"
android:layout_alignParentStart="true" />
</FrameLayout>
答案 1 :(得分:0)
您必须更改代码。
@Override
public void afterTextChanged(Editable s) {
try {
myInputNumber = Double.parseDouble(s.toString());
Double myText = calculations.calculateCurrency(myInputNumber, convertFrom, convertTo);
Toast.makeText(MainActivity.this, myInputNumber+"", Toast.LENGTH_SHORT).show();
//outputNumber.setText(myText+""); // Remove this line
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
}
将TextView(outputNumber)作为参数添加到Constructor。 myCurrencyFetcher fetchCurrency = new myCurrencyFetcher(outputNumber);
class myCurrencyFetcher extends AsyncTask<String, Double, Double>{
private WeakReference<TextView> weakOutputNumber;
public myCurrencyFetcher (TextView outputNumber) {
weakOutputNumber= new WeakReference<TextView>(outputNumber);
}
/* ... Other methods are the same */
@Override
protected void onPostExecute(Double result) {
super.onPostExecute(result);
//Calculations.myText = result;
if (weakOutputNumber != null) {
TextView outputNumber = weakOutputNumber.get();
if (outputNumber != null) {
outputNumber.setText(new String(result));
}
}
}
}