我在 android studio 的 Textview 中显示汇率时遇到问题
I have a problem to showing currency rates in Textview in android studio
我无法在文本视图中显示货币汇率我想显示货币汇率然后我必须计算它在沙特里亚尔和阿拉伯酋长国达勒姆的价值我只使用 11 种货币
我从 3 天开始就解决了它,但它无论如何都不起作用
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonObjectRequest;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Currency;
public class currencconvert extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
private static final String TAG = "tag";
//Url
//this Prayer Timings is only for london now
// Tag used to cancel the request
String tag_json_obj = "json_obj_req";
//progress Dialog
ProgressDialog pDialog;
TextView usdtxt, eurtxt, inrtxt, pkrtxt, afntxt, bdttxt, idrtxt, myrtxt, trytxt, aedtxt, sartxt;
EditText addnum;
Button cnvrtbtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_currencconvert);
usdtxt = findViewById(R.id.usd);
eurtxt = findViewById(R.id.euro);
inrtxt = findViewById(R.id.inr);
pkrtxt = findViewById(R.id.pkr);
afntxt = findViewById(R.id.afn);
bdttxt = findViewById(R.id.bdt);
idrtxt = findViewById(R.id.idr);
myrtxt = findViewById(R.id.myr);
trytxt = findViewById(R.id.tury);
aedtxt = findViewById(R.id.aed);
sartxt = findViewById(R.id.sar);
Spinner spinner = (Spinner) findViewById(R.id.spin);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.currency, android.R.layout.simple_gallery_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
// link wagera he ye
// function to get location
Currencconvert();
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
public void Currencconvert() {
pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();
String Url = "http://data.fixer.io/api/latest?access_key=9ec08275542de6b1646a986ffe785667";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, Url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Toast.makeText(currencconvert.this, "your code run", Toast.LENGTH_SHORT).show();
// get date
//String date = response.getJSONArray("JSON").getJSONObject(0).get("date").toString();
// for getting namaz timings
String usdtx = response.getJSONArray("rates").getJSONObject(0).get("USD").toString();
String eurtx = response.getJSONArray("rates").getJSONObject(0).get("EUR").toString();
int inrtx = Integer.parseInt(response.getJSONArray("rates").getJSONObject(0).get("INR").toString());
int pkrtx = Integer.parseInt(response.getJSONArray("rates").getJSONObject(0).get("PKR").toString());
int afntx = Integer.parseInt(response.getJSONArray("rates").getJSONObject(0).get("AFN").toString());
int bdttx = Integer.parseInt(response.getJSONArray("rates").getJSONObject(0).get("BDT").toString());
int idrtx = Integer.parseInt(response.getJSONArray("rates").getJSONObject(0).get("IDR").toString());
int myrtx = Integer.parseInt(response.getJSONArray("rates").getJSONObject(0).get("MYR").toString());
int trytx = Integer.parseInt(response.getJSONArray("rates").getJSONObject(0).get("TRY").toString());
int aedtx = Integer.parseInt(response.getJSONArray("rates").getJSONObject(0).get("AED").toString());
int sartx = Integer.parseInt(response.getJSONArray("rates").getJSONObject(0).get("SAR").toString());
Toast.makeText(currencconvert.this, "your code in the middle", Toast.LENGTH_SHORT).show();
usdtxt.setText(usdtx);
eurtxt.setText(eurtx);
inrtxt.setText(inrtx);
pkrtxt.setText(pkrtx);
afntxt.setText(afntx);
bdttxt.setText(bdttx);
idrtxt.setText(idrtx);
myrtxt.setText(myrtx);
trytxt.setText(trytx);
aedtxt.setText(aedtx);
sartxt.setText(sartx);
Toast.makeText(currencconvert.this, "congrats your code run completend", Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
pDialog.hide();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(currencconvert.this, "Error", Toast.LENGTH_SHORT).show();
pDialog.hide();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(request, tag_json_obj);
}
}
如果setText方法的参数为整数值,则将其作为要显示的字符串资源的资源标识。所以你应该传递 CharSequence 而不是整数。所以你的代码应该是-
usdtxt.setText(String.valueOf(inrtx));
eurtxt.setText(String.valueOf(pkrtx));
...
我无法在文本视图中显示货币汇率我想显示货币汇率然后我必须计算它在沙特里亚尔和阿拉伯酋长国达勒姆的价值我只使用 11 种货币
我从 3 天开始就解决了它,但它无论如何都不起作用
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonObjectRequest;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Currency;
public class currencconvert extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
private static final String TAG = "tag";
//Url
//this Prayer Timings is only for london now
// Tag used to cancel the request
String tag_json_obj = "json_obj_req";
//progress Dialog
ProgressDialog pDialog;
TextView usdtxt, eurtxt, inrtxt, pkrtxt, afntxt, bdttxt, idrtxt, myrtxt, trytxt, aedtxt, sartxt;
EditText addnum;
Button cnvrtbtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_currencconvert);
usdtxt = findViewById(R.id.usd);
eurtxt = findViewById(R.id.euro);
inrtxt = findViewById(R.id.inr);
pkrtxt = findViewById(R.id.pkr);
afntxt = findViewById(R.id.afn);
bdttxt = findViewById(R.id.bdt);
idrtxt = findViewById(R.id.idr);
myrtxt = findViewById(R.id.myr);
trytxt = findViewById(R.id.tury);
aedtxt = findViewById(R.id.aed);
sartxt = findViewById(R.id.sar);
Spinner spinner = (Spinner) findViewById(R.id.spin);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.currency, android.R.layout.simple_gallery_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
// link wagera he ye
// function to get location
Currencconvert();
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
public void Currencconvert() {
pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();
String Url = "http://data.fixer.io/api/latest?access_key=9ec08275542de6b1646a986ffe785667";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, Url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Toast.makeText(currencconvert.this, "your code run", Toast.LENGTH_SHORT).show();
// get date
//String date = response.getJSONArray("JSON").getJSONObject(0).get("date").toString();
// for getting namaz timings
String usdtx = response.getJSONArray("rates").getJSONObject(0).get("USD").toString();
String eurtx = response.getJSONArray("rates").getJSONObject(0).get("EUR").toString();
int inrtx = Integer.parseInt(response.getJSONArray("rates").getJSONObject(0).get("INR").toString());
int pkrtx = Integer.parseInt(response.getJSONArray("rates").getJSONObject(0).get("PKR").toString());
int afntx = Integer.parseInt(response.getJSONArray("rates").getJSONObject(0).get("AFN").toString());
int bdttx = Integer.parseInt(response.getJSONArray("rates").getJSONObject(0).get("BDT").toString());
int idrtx = Integer.parseInt(response.getJSONArray("rates").getJSONObject(0).get("IDR").toString());
int myrtx = Integer.parseInt(response.getJSONArray("rates").getJSONObject(0).get("MYR").toString());
int trytx = Integer.parseInt(response.getJSONArray("rates").getJSONObject(0).get("TRY").toString());
int aedtx = Integer.parseInt(response.getJSONArray("rates").getJSONObject(0).get("AED").toString());
int sartx = Integer.parseInt(response.getJSONArray("rates").getJSONObject(0).get("SAR").toString());
Toast.makeText(currencconvert.this, "your code in the middle", Toast.LENGTH_SHORT).show();
usdtxt.setText(usdtx);
eurtxt.setText(eurtx);
inrtxt.setText(inrtx);
pkrtxt.setText(pkrtx);
afntxt.setText(afntx);
bdttxt.setText(bdttx);
idrtxt.setText(idrtx);
myrtxt.setText(myrtx);
trytxt.setText(trytx);
aedtxt.setText(aedtx);
sartxt.setText(sartx);
Toast.makeText(currencconvert.this, "congrats your code run completend", Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
pDialog.hide();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(currencconvert.this, "Error", Toast.LENGTH_SHORT).show();
pDialog.hide();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(request, tag_json_obj);
}
}
如果setText方法的参数为整数值,则将其作为要显示的字符串资源的资源标识。所以你应该传递 CharSequence 而不是整数。所以你的代码应该是-
usdtxt.setText(String.valueOf(inrtx));
eurtxt.setText(String.valueOf(pkrtx));
...