我在androidstudio中使用改型从mysql检索数据。我想在recyclerview中显示数据。数据是 id_departure
= K1
(字符串), id_destination
= K2
(字符串), schedule
= 2020-11-23 12:00:00
(日期时间)。我可以在android studio中以不同的值显示数据吗?例如: K1
代表伦敦, K2
代表伯明翰, K3
代表曼彻斯特, date
=2020年11月23日, time
= 12:00. 有人知道怎么做吗?谢谢您。以下是我在myorderactivity中的代码:
public class MyOrderActivity extends AppCompatActivity {
private RecyclerView rvHistory;
private RecyclerView.Adapter historyAdapter;
private RecyclerView.LayoutManager lmHistory;
private List<HistoryData> listData = new ArrayList<>();
private SwipeRefreshLayout swipeRefreshLayout;
private ProgressBar progressBar;
SessionManager sessionManager;
String idUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_order);
rvHistory = findViewById(R.id.rv_history);
lmHistory = new LinearLayoutManager(MyOrderActivity.this, LinearLayoutManager.VERTICAL, false);
rvHistory.setLayoutManager(lmHistory);
swipeRefreshLayout = findViewById(R.id.swipe_refresh);
progressBar = findViewById(R.id.progress_bar);
sessionManager = new SessionManager(MyOrderActivity.this);
idUser = sessionManager.getUserDetail().get(SessionManager.ID_USERS);
showRecyclerList(idUser);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
swipeRefreshLayout.setRefreshing(true);
showRecyclerList(idUser);
swipeRefreshLayout.setRefreshing(false);
}
});
}
private void showRecyclerList(String idUser) {
progressBar.setVisibility(View.VISIBLE);
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<History> historyCall = apiInterface.historyResponse(idUser);
historyCall.enqueue(new Callback<History>() {
@Override
public void onResponse(Call<History> call, Response<History> response) {
if(response.body() != null && response.isSuccessful() && response.body().isStatus()) {
String message = response.body().getMessage();
Toast.makeText(MyOrderActivity.this, message, Toast.LENGTH_SHORT).show();
listData = response.body().getData();
historyAdapter = new HistoryAdapter(MyOrderActivity.this, listData);
rvHistory.setAdapter(historyAdapter);
historyAdapter.notifyDataSetChanged();
progressBar.setVisibility(View.INVISIBLE);
}
}
@Override
public void onFailure(Call<History> call, Throwable t) {
progressBar.setVisibility(View.INVISIBLE);
Toast.makeText(MyOrderActivity.this, t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
历史数据.java
public class HistoryData {
@SerializedName("schedule")
private String schedule;
@SerializedName("id_departure")
private String idDepature;
@SerializedName("id_destination")
private String idDestination;
@SerializedName("id_users")
private String idUsers;
public void setSchedule(String schedule){
this.schedule = schedule;
}
public String getSchedule(){
return schedule;
}
public void setIdDeparture(String idDeparture){
this.idDeparture = idDeparture;
}
public String getIdDeparture(){
return idDeparture;
}
public void setIdDestination(String idDestination){
this.idDestination = idDestination;
}
public String getIdDestination(){
return idDestination;
}
public void setIdUsers(String idUsers){
this.idUsers = idUsers;
}
public String getIdUsers(){
return idUsers;
}
public HistoryData(){
}
}
历史.java
public class History{
@SerializedName("data")
private List<HistoryData> data;
@SerializedName("message")
private String message;
@SerializedName("status")
private boolean status;
public void setData(List<HistoryData> data){
this.data = data;
}
public List<HistoryData> getData(){
return data;
}
public void setMessage(String message){
this.message = message;
}
public String getMessage(){
return message;
}
public void setStatus(boolean status){
this.status = status;
}
public boolean isStatus(){
return status;
}
}
历史适配器.java
public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.HistoryHolder> {
private Context ctx;
private List<HistoryData> listHistory;
public HistoryAdapter(Context ctx, List<HistoryData> listHistory) {
this.ctx = ctx;
this.listHistory = listHistory;
}
@NonNull
@Override
public HistoryHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cardview_history, parent, false);
HistoryHolder holder = new HistoryHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull final HistoryHolder holder, int position) {
HistoryData historyData = listHistory.get(position);
holder.tvDeparture.setText(historyData.getIdDeparture());
holder.tvDestination.setText(historyData.getIdDestination());
holder.tvDate.setText(historyData.getSchedule());
holder.tvTime.setText(historyData.getSchedule());
}
@Override
public int getItemCount() {
return listHistory.size();
}
public class HistoryHolder extends RecyclerView.ViewHolder {
TextView tvDeparture, tvDestination, tvDate, tvTime;
public HistoryHolder(@NonNull View itemView) {
super(itemView);
tvDeparture = itemView.findViewById(R.id.tv_item_departure);
tvDestination = itemView.findViewById(R.id.tv_item_destination);
tvDate = itemView.findViewById(R.id.tv_item_date);
tvTime = itemView.findViewById(R.id.tv_item_time);
}
}
}
暂无答案!
目前还没有任何答案,快来回答吧!