package com.example.metro_info_front_end;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.metro_info_front_end.DataModel.MetroStation;
import com.example.metro_info_front_end.api.ApiService;
import com.example.metro_info_front_end.api.RetrofitClient;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class MainActivity extends AppCompatActivity {
private LinearLayout llOptimalRoute; // 最优路线查询界面布局
private LinearLayout llMetroLine; // 地铁线路查询界面布局
private AutoCompleteTextView systemName1;
private AutoCompleteTextView systemName2;
private AutoCompleteTextView lineName;
private AutoCompleteTextView departureStation;
private AutoCompleteTextView destinationStation;
private ApiService apiService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取布局中的线性布局
llOptimalRoute = findViewById(R.id.LL1);
llMetroLine = findViewById(R.id.LL2);
// 获取按钮
Button btnOptimalRoute = findViewById(R.id.btn1);
Button btnMetroLine = findViewById(R.id.btn2);
// 设置按钮点击事件监听器
btnOptimalRoute.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 显示最优路线查询界面,隐藏地铁线路查询界面
llOptimalRoute.setVisibility(View.VISIBLE);
llMetroLine.setVisibility(View.GONE);
}
});
btnMetroLine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 显示地铁线路查询界面,隐藏最优路线查询界面
llOptimalRoute.setVisibility(View.GONE);
llMetroLine.setVisibility(View.VISIBLE);
}
});
// 获取 AutoCompleteTextView 控件
systemName1 = findViewById(R.id.SystemName1);
systemName2 = findViewById(R.id.SystemName2);
lineName = findViewById(R.id.LineName);
departureStation = findViewById(R.id.DepartureStation);
destinationStation = findViewById(R.id.DestinationStation);
// 设置自动完成功能适配器
ArrayAdapter<String> cityAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, getCityData());
ArrayAdapter<String> lineAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, getLineData());
ArrayAdapter<String> stationAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, getStationData());
systemName1.setAdapter(cityAdapter);
systemName2.setAdapter(cityAdapter);
lineName.setAdapter(lineAdapter);
departureStation.setAdapter(stationAdapter);
destinationStation.setAdapter(stationAdapter);
// 创建 ApiService 实例
apiService = RetrofitClient.getRetrofitInstance().create(ApiService.class);
// 设置查询按钮点击事件监听器
Button query1 = findViewById(R.id.query1);
Button query2 = findViewById(R.id.query2);
query1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 获取用户输入的城市、出发站点和目标站点信息
String city1 = systemName1.getText().toString();
String departure = departureStation.getText().toString();
String destination = destinationStation.getText().toString();
// 在新线程中执行网络请求
new Thread(new Runnable() {
@Override
public void run() {
// 创建 Retrofit 实例
ApiService apiService = RetrofitClient.getRetrofitInstance().create(ApiService.class);
// 发起网络请求获取最优路线查询结果
Call<List<MetroStation>> call = apiService.searchShortestPath(city1, departure, destination);
call.enqueue(new Callback<List<MetroStation>>() {
@Override
public void onResponse(Call<List<MetroStation>> call, Response<List<MetroStation>> response) {
if (response.isSuccessful() && response.body() != null) {
List<MetroStation> stations = response.body();
// 处理查询结果,更新界面或显示信息
final StringBuilder resultBuilder = new StringBuilder();
for (MetroStation station : stations) {
resultBuilder.append(station.getStationName()).append("\n");
}
// 切换回主线程更新 UI
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, resultBuilder.toString(), Toast.LENGTH_LONG).show();
}
});
} else {
// 切换回主线程提示查询失败
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "查询失败", Toast.LENGTH_SHORT).show();
}
});
}
}
@Override
public void onFailure(Call<List<MetroStation>> call, Throwable t) {
// 切换回主线程提示网络请求失败
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "网络请求失败", Toast.LENGTH_SHORT).show();
}
});
}
});
}
}).start(); // 启动新线程
}
});query1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 获取用户输入的城市、出发站点和目标站点信息
String city1 = systemName1.getText().toString();
String departure = departureStation.getText().toString();
String destination = destinationStation.getText().toString();
// 在新线程中执行网络请求
new Thread(new Runnable() {
@Override
public void run() {
// 创建 Retrofit 实例
ApiService apiService = RetrofitClient.getRetrofitInstance().create(ApiService.class);
// 发起网络请求获取最优路线查询结果
Call<List<MetroStation>> call = apiService.searchShortestPath(city1, departure, destination);
call.enqueue(new Callback<List<MetroStation>>() {
@Override
public void onResponse(Call<List<MetroStation>> call, Response<List<MetroStation>> response) {
if (response.isSuccessful() && response.body() != null) {
List<MetroStation> stations = response.body();
// 处理查询结果,更新界面或显示信息
final StringBuilder resultBuilder = new StringBuilder();
for (MetroStation station : stations) {
resultBuilder.append(station.getStationName()).append("\n");
}
// 切换回主线程更新 UI
runOnUiThread(new Runnable() {
@Override
public void run() {
// 获取显示结果的 TextView 控件
TextView resultTextView = findViewById(R.id.resultTextView);
// 将查询结果设置到 TextView 中
resultTextView.setText(resultBuilder.toString());
}
});
} else {
// 切换回主线程提示查询失败
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "查询失败", Toast.LENGTH_SHORT).show();
}
});
}
}
@Override
public void onFailure(Call<List<MetroStation>> call, Throwable t) {
// 切换回主线程提示网络请求失败
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "网络请求失败", Toast.LENGTH_SHORT).show();
}
});
}
});
}
}).start(); // 启动新线程
}
});
query2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 获取用户输入的城市和线路名称信息
String city2 = systemName2.getText().toString();
String lineName1 = lineName.getText().toString();
// 在新线程中执行网络请求
new Thread(new Runnable() {
@Override
public void run() {
// 创建 Retrofit 实例
ApiService apiService = RetrofitClient.getRetrofitInstance().create(ApiService.class);
// 发起网络请求获取查询结果
Call<List<MetroStation>> call = apiService.getStationsBySystemAndLine(city2, lineName1);
call.enqueue(new Callback<List<MetroStation>>() {
@Override
public void onResponse(Call<List<MetroStation>> call, Response<List<MetroStation>> response) {
if (response.isSuccessful() && response.body() != null) {
List<MetroStation> stations = response.body();
// 处理查询结果,更新界面或显示信息
final StringBuilder resultBuilder = new StringBuilder();
for (MetroStation station : stations) {
resultBuilder.append(station.getStationName()).append("\n");
}
// 切换回主线程更新 UI
runOnUiThread(new Runnable() {
@Override
public void run() {
// 获取显示结果的 TextView 控件
TextView resultTextView = findViewById(R.id.resultTextView);
// 将查询结果设置到 TextView 中
resultTextView.setText(resultBuilder.toString());
}
});
} else {
// 切换回主线程提示查询失败
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "查询失败", Toast.LENGTH_SHORT).show();
}
});
}
}
@Override
public void onFailure(Call<List<MetroStation>> call, Throwable t) {
// 切换回主线程提示网络请求失败
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "网络请求失败", Toast.LENGTH_SHORT).show();
}
});
}
});
}
}).start(); // 启动新线程
}
});
}
// 模拟城市数据
private String[] getCityData() {
return new String[]{"石家庄"};
}
private String[] getLineData() {
return new String[]{"一号线"};
}
private String[] getStationData() {
return new String[]{"新百广场", "北国商城"};
}
}
标签:void,查询,MainActivity,地铁,import,Override,new,Android,public
From: https://www.cnblogs.com/muzhaodi/p/18136902