我知道这是一个愚蠢的错误,这是一个新手的错误,但我无法解决。问题是arraylists的值似乎是空的onCreate在按钮的onclick下面。但是当我点击按钮我看到他们的价值观在调试器上。如何在不使用onclick或其他东西的情况下使用它们?我需要帮助。
ArrayList<String> menkulList = new ArrayList<>();
ArrayList<String> miktarList = new ArrayList<>();
ArrayList<String> fiyatList = new ArrayList<>();
ArrayList<Double> tutarList = new ArrayList<>();
private TextView tx;
Button btn;
TableLayout table;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.portfoy);
table = (TableLayout) findViewById(R.id.customer_table);
btn = (Button) findViewById(R.id.tikla);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for(String s : menkulList) {
Log.i("Menkul list", s);
}
for(String s : miktarList) {
Log.i("Miktar list", s);
}
for(String s: fiyatList) {
Log.i("Fiyat list", s);
}
for(Double d: tutarList) {
Log.i("Tutar list", d.toString());
}
}
});
String jsonUrl = createJsonUrl();
jsonProcess(jsonUrl);
for(String s : menkulList) {
Log.i("Menkul list", s);
}
for(String s : miktarList) {
Log.i("Miktar list", s);
}
for(String s: fiyatList) {
Log.i("Fiyat list", s);
}
for(Double d: tutarList) {
Log.i("Tutar list", d.toString());
}
}
public void jsonProcess(String jsonString){
RequestQueue queue = Volley.newRequestQueue(this);
String url = jsonString;
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
try {
JSONObject jObj = new JSONObject(response);
JSONArray jArr = jObj.getJSONArray("Item");
for (int i = 0; i < jArr.length(); i++) {
jObj = jArr.getJSONObject(i);
String s1 = jObj.getString("Symbol");
String s2 = jObj.getString("Qty_T2");
String s3 = jObj.getString("LastPx");
Double tutar = Double.parseDouble(s2) * Double.parseDouble(s3);
fillArrayLists(s1,s2,s3,tutar);
}
}catch(Exception e){
Log.i("Error while json parse",e.toString());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("Error is",error.toString());
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
}
public void fillArrayLists(String menkul,String miktar,String fiyat,Double tutar){
menkulList.add(menkul);
miktarList.add(miktar);
fiyatList.add(fiyat);
tutarList.add(tutar);
fillTableLayout();
}
public void fillTableLayout(){
}
public String createJsonUrl(){
Intent i = getIntent();
String customerId = i.getStringExtra(MainActivity.CUSTOMER_ID);
String jsonString = "https://tb.matriksdata.com/9999/Integration.aspx?MsgType=AN&CustomerNo=0&Username=proje&Password=proje&AccountID="+ customerId +"&ExchangeID=4&OutputType=2";
Log.i("Json String is : ", jsonString);
return jsonString;
}