我正在尝试创建一个应用程序,它将显示列车名称以及列车编号。
以下是我使用的示例:Bakerloo Line
我正在尝试插入搜索功能,但我不确定如何使其正常工作。
目前我正在使用IF语句尝试按行号过滤数据。它的工作分类......
我得到相关的timeToStation
。但是,currentLocation
和towards
与搜索的第一项保持一致。
package com.example.devan.tfldriver_advanced;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.sql.Array;
import java.util.ArrayList;
public class linesactivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private RequestQueue mQueue;
//Global declaration
String linename,result;
TextView spinnerview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lines);
TextView mTextview = findViewById(R.id.lines);
//Uline will change to each specific line
Intent intent = getIntent();
linename = intent.getStringExtra("Uline");
//Produces title
String Title = linename;
Title = Title.replaceAll("_", " ");
mTextview.setText(Title + " Line");
Intent b = getIntent();
int a = b.getIntExtra("Ustation",-1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, a, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setOnItemSelectedListener(this);
spinner.setAdapter(adapter);
mQueue = Volley.newRequestQueue(this);
//the parse is the update button
Button button_parse = findViewById(R.id.button_parse);
//when the button is clicked run the following method
button_parse.setOnClickListener(newView.OnClickListener() {
@Override
public void onClick(View v) {
jsonParse();
}
});
}
private void jsonParse() {
String url = "https://api.tfl.gov.uk/line/" + linename + "/arrivals";
JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//the user enters the train number
EditText train_number;
train_number = findViewById(R.id.train_number);
String str = train_number.getText().toString();
String sta = spinnerview.getText().toString();
String emty = train_number.getText().toString();
try {
TextView mTextViewResult;
mTextViewResult = findViewById(R.id.text_view_result);
for (int i = 0; i < response.length(); i++) {
JSONObject x = response.getJSONObject(i);
String vehicleId = x.getString("vehicleId");
String currentLocation = x.getString("currentLocation");
String towards = x.getString("towards");
Integer timeToStation = x.getInt("timeToStation");
if (vehicleId.equals(str)) {
mTextViewResult.append(" Train number " + vehicleId + "\n" + " Currently location " + currentLocation + "\n" + " going to " + towards + " in " + timeToStation / 60 + " min" + "\n\n");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mQueue.add(request);
}
@Override
public void onItemSelected(final AdapterView<?> parent,View view, int i, long l) {
result = spinner.getSelectedItem().toString();
spinnerview = (TextView)findViewById(R.id.spinnerview);
spinnerview.setText(result);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}