我设法在ListView上添加了一个带有启动和停止按钮的计时器,所有工作正常,但是当我按下或关闭应用程序时,计时器值将重置为00:00。我需要让它继续计数,即使应用程序关闭,直到我回来,我停止它。如果有人知道怎么做,他/她的帮助将不胜感激。
以下是ListviewAdapter
...............
viewHolder = new Holder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.payment_timing_list_adapter_view, parent, false);
viewHolder.payment_id = (TextView) convertView.findViewById(R.id.paymId);
viewHolder.history_id = (TextView) convertView.findViewById(R.id.histId);
viewHolder.drivernames = (TextView) convertView.findViewById(R.id.drivername);
viewHolder.plateno = (TextView) convertView.findViewById(R.id.plateno);
viewHolder.parkingname = (TextView) convertView.findViewById(R.id.pname);
viewHolder.phone = (TextView) convertView.findViewById(R.id.phonNo);
viewHolder.chronometer = (Chronometer) convertView.findViewById(R.id.chronometer);
viewHolder.button_start = (Button) convertView.findViewById(R.id.button5);
viewHolder.button_leave = (Button) convertView.findViewById(R.id.button6);
// SQLite database handler
db = new SQLiteHelper(getContext());
convertView.setTag(viewHolder);
} else {
viewHolder = (Holder) convertView.getTag();
}
viewHolder.drivernames.setText(paymentTiming_items.getDriverFullName());
viewHolder.plateno.setText(paymentTiming_items.getPlateNo());
viewHolder.parkingname.setText(paymentTiming_items.getParkName());
viewHolder.payment_id.setText(paymentTiming_items.getPaymentId());
viewHolder.history_id.setText(paymentTiming_items.getHistoryId());
viewHolder.phone.setText(paymentTiming_items.getDriverPhone());
viewHolder.chronometer.setText(paymentTiming_items.getChronoTime());
// Start chronometer on button click
viewHolder.button_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewHolder.chronometer.setBase(SystemClock.elapsedRealtime());
viewHolder.chronometer.start();
// Toast.makeText(getContext(), position, Toast.LENGTH_SHORT).show();
Log.d("My POSITION", "" + position);
}
});
// Stop chronometer on button click
viewHolder.button_leave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String paymentID = viewHolder.payment_id.getText().toString();
String historyId = viewHolder.history_id.getText().toString();
String driver_phonenumber = viewHolder.phone.getText().toString();
viewHolder.chronometer.stop();
time_parked = SystemClock.elapsedRealtime() - viewHolder.chronometer.getBase();
time_parked = (time_parked / 1000) / 60;
Log.d("time elapsed", "" + time_parked);
..............
显示列表视图的主要活动
.........................
private void FindDriver_Timing(final String custodian_id) {
// Tag used to cancel the request
String tag_string_req = "req_Verfication";
progressBarList.setVisibility(View.VISIBLE);
StringRequest strReq = new StringRequest(Request.Method.POST,
Urls.payementUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "cerfication Response: " + response.toString());
JSONArray jsonArrayResult ;
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
// Check for error node in json
if (!error) {
List<PaymentTiming_Items> getAllDiverDetails = new
ArrayList<PaymentTiming_Items>();
jsonArrayResult = jObj.getJSONArray("user");
for(int x=0; x<jsonArrayResult.length();x++) {
JSONObject json =
jsonArrayResult.getJSONObject(x);
String driver_fullname =
json.getString("driver_fullname");
String driver_phonenumber =
json.getString("driver_phonenumber");
String plate_no = json.getString("plate_no");
String parking_name =
json.getString("parking_name");
String historyId = json.getString("historyId");
String paymentId = json.getString("paymentId");
PaymentTiming_Items timingItems = new
PaymentTiming_Items();
timingItems.setPlateNo(plate_no);
timingItems.setParkingName(parking_name);
timingItems.setDriverFullName(driver_fullname);
timingItems.setHistoryId(historyId);
timingItems.setPaymentId(paymentId);
timingItems.setDriverphone(driver_phonenumber);
getAllDiverDetails.add(timingItems); // store all drivers' info to
userList = new ArrayList<>(getAllDiverDetails);
ShowListData();
}
progressBarList.setVisibility(View.GONE);
}
else{
// Error in login. Get the error message
// hiding the progress bar
progressBarList.setVisibility(View.GONE);
myList.setVisibility(View.VISIBLE);
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(), errorMsg, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Json error: " +
e.getMessage(), Toast.LENGTH_SHORT).show();
// // hiding the progress bar
progressBarList.setVisibility(View.GONE);
myList.setVisibility(View.VISIBLE);
}
}
}
}, new Response.ErrorListener() {
.........................