我怎样才能从mysql数据库获取的整数转换为时间格式?这是我的java文件
带arrayAdapter的parking_records.java
public class parking_records extends ArrayAdapter<String> {
private String[] Parking_Start_Time;
private String[] Parking_End_Time;
private String[] Duration;
private Activity context;
public parking_records(Activity context, String[] Parking_Start_Time, String[] Parking_End_Time, String[] Duration) {
super(context, R.layout.fragment_parking_record, Parking_Start_Time);
this.context = context;
this.Parking_Start_Time = Parking_Start_Time;
this.Parking_End_Time = Parking_End_Time;
this.Duration = Duration;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.fragment_parking_record, null, true);
TextView textViewPRd = (TextView) listViewItem.findViewById(R.id.textViewPRd);
TextView textViewPRd1 = (TextView) listViewItem.findViewById(R.id.textViewPRd1);
TextView textViewPRr = (TextView) listViewItem.findViewById(R.id.textViewPRr);
textViewPRd.setText(Parking_Start_Time[position]);
textViewPRd1.setText(Parking_End_Time[position]);
textViewPRr.setText(Duration[position]);
return listViewItem;
}
}
Parking_Records.java
public class Parking_Records {
public static String[] Parking_Start_Time;
public static String[] Parking_End_Time;
public static String[] Duration;
public static final String JSON_ARRAY = "result";
public static final String KEY_ParkStartTime = "Parking_Start_Time";
public static final String KEY_ParkEndTime = "Parking_End_Time";
public static final String KEY_ParkDuration = "Duration";
private JSONArray users = null;
private String json;
public Parking_Records(String json){
this.json = json;
}
public void parseJSON(){
JSONObject jsonObject=null;
try {
jsonObject = new JSONObject(json);
users = jsonObject.getJSONArray(JSON_ARRAY);
Parking_Start_Time = new String[users.length()];
Parking_End_Time = new String[users.length()];
Duration = new String[users.length()];
for(int i=0;i<users.length();i++){
JSONObject jo = users.getJSONObject(i);
Parking_Start_Time[i] = jo.getString(KEY_ParkStartTime);
Parking_End_Time[i] = jo.getString(KEY_ParkEndTime);
Duration[i] = jo.getString(KEY_ParkDuration);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
主Parking_Records_Fragment.java
public class ParkingRecordFragment extends Fragment implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {
ProgressDialog dialog;
String url;
SessionManager session;
private String Username;
private String Acc_Pass;
SharedPreferences shared;
private SwipeRefreshLayout mSwipeRefreshLayout;
public ParkingRecordFragment() {
// Required empty public constructor
}
private ListView listView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("Parking Record");
// Session class instance
session = new SessionManager(getActivity().getApplicationContext());
dialog = new ProgressDialog(getActivity());
dialog.setMessage("Loading....");
dialog.show();
View rootView = inflater.inflate(R.layout.activity_listview_parking_records, container, false);
listView = (ListView) rootView.findViewById(R.id.listView);
listView.setEmptyView(rootView.findViewById(R.id.empty_list_item));
mSwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh_layout);
mSwipeRefreshLayout.setOnRefreshListener(this);
mSwipeRefreshLayout.postDelayed(new Runnable() {
@Override
public void run() {
mSwipeRefreshLayout.setRefreshing(false);
sendRequest(Username, Acc_Pass);
}
}, 1000);
sendRequest(Username, Acc_Pass);
shared= getActivity().getSharedPreferences("Mypref", Context.MODE_PRIVATE);
return rootView;
}
@Override
public void onRefresh() {
sendRequest(Username, Acc_Pass);
}
private void sendRequest(String Username, String Acc_Pass){
HashMap<String, String> user = session.getUserDetails();
Username = user.get(SessionManager.KEY_USERNAME);
Acc_Pass = user.get(SessionManager.KEY_PASSWORD);
RequestQueue requestQueue = VolleyController.getInstance(getActivity().getApplicationContext()).getRequestQueue();
String url = "http://192.168.1.5/json_parking_records2.php?Username="+Username+"&Acc_Pass="+Acc_Pass+"";
url = url.replaceAll(" ", "%20");
try {
URL sourceUrl = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
Log.i("Getting url info",""+url);
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
showJSON(response);
dialog.dismiss();
mSwipeRefreshLayout.setRefreshing(false);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity().getApplicationContext(),error.getMessage(),Toast.LENGTH_LONG).show();
dialog.dismiss();
}
});
requestQueue.add(stringRequest);
}
private void showJSON(String json){
Parking_Records pj = new Parking_Records(json);
pj.parseJSON();
parking_records cl = new parking_records(getActivity(), Parking_Records.Parking_Start_Time, Parking_Records.Parking_End_Time, Parking_Records.Duration);
listView.setAdapter(cl);
}
@Override
public void onClick(View v) {
sendRequest(Username, Acc_Pass);
}
}
Android screenshot
所以现在我一直在努力如何将整数从时间格式转换为时间格式。请指导我。谢谢
最佳答案
我已经成功地解决了自己的问题。这是代码。
textViewPRr.setText(String.format("%d:%02d:%02d", (Duration[position]/3600), (Duration[position]%3600)/60, (Duration[position]%60)));
我使用arrayAdapter在parking_records.java中设置了它
希望这个答案可以帮助别人!
关于java - Android将mysql数据库中的int数转换为时间格式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46327989/