これに適切にアプローチする方法について、誰かが私に何かアドバイスをくれますか?
ここで問題です。まずビールを飲みます。説明する私の試みは冷たいものを必要とするかもしれません:)
サーバーからダウンロードされたJSONを介して取り込まれているListViewがあります。
このリストビューには pickup_time (文字列) があり、現在の時刻と pickup_time の時間差を計算しています
私がやろうとしているのは、textView と textView.setBackgroundColor を使用して計算した時間の差に応じて、割り当てられた pickup_time に緑、黄、または赤の円のイメージをロードすることです。リストビューで。
ListView WASは正しく機能し、情報を正しく表示していました。最近、緑/黄/赤の画像を対応する pickup_time 文字列に追加しようとしただけですが、これがクラッシュする場所であり、助けが必要です。
それでは、いくつかのコードをご紹介します!
ここでは、AsyncTask を使用して listView にデータを入力しています。気がつけば、私はparse_ready_at(JobsArray, i1);を持っています。ここで時差を計算しています。
public class Jobs extends ListActivity {
String NEW_JOB = " ";
Vibrator vib;
boolean reloadOnResume;
TextView assigned;
static ProjectDebug LOGCAT = new ProjectDebug();
ProgressDialogManager pDialog = new ProgressDialogManager();
static String JOB, ON_TIME_PERFORMANCE;
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList;
int PU_time_until_late;
int DEL_time_until_late;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.jobs);
getWindow().setLayout (LayoutParams.FILL_PARENT /* width */ , LayoutParams.WRAP_CONTENT /* height */);
vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
assigned = (TextView) findViewById(R.id.assigned);
RELOAD = true;
Jobs.this.setTitle("My Jobs");
reloadOnResume = false;
VerifyDriverCredentials();
// selecting single ListView item
final ListView lv = getListView();
// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long ide) {
vib.vibrate(40);
// Starting new intent
try { Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra("jobInfo", JOBS.getJSONObject(position).toString());
in.putExtra(pays, JOBS.getJSONObject(position).toString());
in.putExtra(customer_name, JOBS.getJSONObject(position).toString());
in.putExtra(job, JOBS.getJSONObject(position).toString());
in.putExtra(ready_at, JOBS.getJSONObject(position).toString());
in.putExtra(due_by, JOBS.getJSONObject(position).toString());
in.putExtra(customer_reference, JOBS.getJSONObject(position).toString());
in.putExtra(pieces, JOBS.getJSONObject(position).toString());
in.putExtra(weight, JOBS.getJSONObject(position).toString());
in.putExtra(signature_required, JOBS.getJSONObject(position).toString());
in.putExtra(acknowledged, JOBS.getJSONObject(position).toString());
in.putExtra(pickup_actual_datetime, JOBS.getJSONObject(position).toString());
// Pickup Info
in.putExtra(pickup_name, JOBS.getJSONObject(position).toString());
in.putExtra(pickup_addr1, JOBS.getJSONObject(position).toString());
in.putExtra(pickup_city, JOBS.getJSONObject(position).toString());
in.putExtra(pickup_state, JOBS.getJSONObject(position).toString());
in.putExtra(pickup_to_see, JOBS.getJSONObject(position).toString());
in.putExtra(pickup_room, JOBS.getJSONObject(position).toString());
in.putExtra(pickup_phone, JOBS.getJSONObject(position).toString());
in.putExtra(pickup_zip_postal, JOBS.getJSONObject(position).toString());
in.putExtra(pickup_special_instr, JOBS.getJSONObject(position).toString());
// Deliver Info
in.putExtra(deliver_name, JOBS.getJSONObject(position).toString());
in.putExtra(deliver_addr1, JOBS.getJSONObject(position).toString());
in.putExtra(deliver_city, JOBS.getJSONObject(position).toString());
in.putExtra(deliver_state, JOBS.getJSONObject(position).toString());
in.putExtra(deliver_zip_postal, JOBS.getJSONObject(position).toString());
in.putExtra(deliver_to_see, JOBS.getJSONObject(position).toString());
in.putExtra(deliver_room, JOBS.getJSONObject(position).toString());
in.putExtra(deliver_special_instr, JOBS.getJSONObject(position).toString());
in.putExtra(deliver_phone, JOBS.getJSONObject(position).toString());
startActivity(in);
Jobs.this.overridePendingTransition(R.anim.fadein, R.anim.fadeout);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private void VerifyDriverCredentials() {
if (jobs_assigned == 0){
assigned.setVisibility(View.VISIBLE);
assigned.setText("You have no jobs assigned");
GetWindowParameters();
}
if (jobs_assigned > 0 && reloadOnResume == false) {
assigned.setVisibility(View.GONE);
new ParseJobs().execute();
}
}
public class ParseJobs extends AsyncTask<Void, Void, Void> {
String DEL_late = "del_late";
String PU_late = " pu_late";
int i1 = 0;
@Override
protected void onPreExecute() {
pDialog.showProgressDialog(Jobs.this, "Performing calculations", "Loading... Please Wait...");
}
@Override
protected Void doInBackground(Void... params) {
contactList = new ArrayList<HashMap<String, String>>();
// looping through All Contacts
try { for (i1 = 0; i1 < JOBS.length(); i1++) {
JobsArray = JOBS.getJSONObject(i1);
JOB = JobsArray.getString(job);
ON_TIME_PERFORMANCE = JobsArray.getString(on_time_performance);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(job, JobsArray.getString(job));
map.put(pays, JobsArray.getString(pays));
map.put(ready_at, JobsArray.getString(ready_at));
map.put(due_by, JobsArray.getString(due_by));
map.put(new_job, JobsArray.getString(new_job));
//map.put(PU_late, Integer.toString(PU_time_until_late));
//map.put(DEL_late, Integer.toString(DEL_time_until_late));
// adding HashList to ArrayList
contactList.add(map);
}
} catch (JSONException e) {
}
parse_ready_at(JobsArray, i1);
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (JOB != null) { assigned.setText(""); }
else { assigned.setText("You have no jobs assigned"); }
GetWindowParameters();
/** Updating parsed JSON data into ListView */
ListAdapter adapter = new SimpleAdapter(Jobs.this, contactList, R.layout.list_item, new String[] { job, pays, ready_at, due_by, new_job, PU_late, DEL_late},
new int[] { R.id.job1, R.id.pays1, R.id.ready_at1, R.id.due_by1, R.id.newjob1, R.id.imageViewReadyAt, R.id.imageViewDueBy });
TextView imageViewReadyAt = (TextView) findViewById(R.id.imageViewReadyAt);
//Change color/answer/etc for textView_5
if ( PU_time_until_late > 60) { // if more than 60 minutes
imageViewReadyAt.setBackgroundResource(R.drawable.notification);
}
if ( PU_time_until_late < 60) { //if less than 60
imageViewReadyAt.setBackgroundResource(R.drawable.green_light);
}
if ( PU_time_until_late < 30) { // if less than 30
imageViewReadyAt.setBackgroundResource(R.drawable.yellow_light);
}
if ( PU_time_until_late < 1) { // if less than 1 minutes
imageViewReadyAt.setBackgroundResource(R.drawable.red_light);
}
setListAdapter(adapter);
pDialog.dismissProgressDialog(Jobs.this);
}
}
public void parse_ready_at(JSONObject JobsArray, int i1) {
String parse_ready_at_ARRAY;
String tracking_Number;
try { for (i1 = 0; i1 < JOBS.length(); i1++) {
JobsArray = JOBS.getJSONObject(i1);
parse_ready_at_ARRAY = JobsArray.getString(ready_at);
tracking_Number = JobsArray.getString(job);
//Example of ready_at String --> "ready_at": "07/25/2012 08:26:00 PM" we split time from date to get time only
SimpleDateFormat parserSDF = new SimpleDateFormat("M/d/yyyy hh:mm:ss a"); // <--- Correct format to read "lastLatitudeUpdate"
try { parserSDF.parse(parse_ready_at_ARRAY);
}
catch (ParseException e) { LOGCAT.DEBUG("JOBS parse_ready_at", "Error parsing tracking number = " + tracking_Number + " ready_at Array = " + parse_ready_at_ARRAY + " Error = " + e.toString()); }
/* ################################################
* #### DISSECTING READY_AT STRING ARRAY ###
* ################################################
*/
String[] dissect_ready_at_DATE_TIME = parse_ready_at_ARRAY.split(" "); // Splitting space between 07/25/2012 and 08:26:00 and PM
String get_ready_at_DATE = String.valueOf(dissect_ready_at_DATE_TIME[0]); // Set at 0 because we want date. 07-25-2012
String get_ready_at_TIME = String.valueOf(dissect_ready_at_DATE_TIME[1]); // Set at 1 because we want time. 08:26:00
String get_ready_at_AMPM = String.valueOf(dissect_ready_at_DATE_TIME[2]); // Set at 2 because we want AM PM.
/* ########################################################
* #### GETTING DATE FROM READY_AT STRING ARRAY ###
* ########################################################
*/
String[] dissect_ready_at_DATE = get_ready_at_DATE.split("/"); // Splitting the / between 07 and 25 and 2012 from 07/25/2012
int get_ready_at_MONTH = Integer.valueOf(dissect_ready_at_DATE[0]); // Set at 0 because we want month. 07
int get_ready_at_DAY = Integer.valueOf(dissect_ready_at_DATE[1]); // Set at 1 because we want day. 25
int get_ready_at_YEAR = Integer.valueOf(dissect_ready_at_DATE[2]); // Set at 2 because we want yeay. 2012
/* ########################################################
* #### GETTING TIME FROM READY_AT STRING ARRAY ###
* ########################################################
*/
String[] dissect_ready_at_TIME = get_ready_at_TIME.split(":"); // Splitting the : between 08 and 26 and 00
int get_ready_at_HOUR = Integer.valueOf(dissect_ready_at_TIME[0]); // Set at 0 because we want hour. 08
int get_ready_at_MINUTE = Integer.valueOf(dissect_ready_at_TIME[1]); // Set at 1 because we want minute. 26
/* ################################################################
* #### CONVERT HOUR FROM READY_AT STRING TO MILITARY TIME ###
* ################################################################
*/
int convert_ready_at_HOUR_to_military = 0; // By default, ready_at String is in 12 hour format. we need to fix it so it is military time.
if (get_ready_at_AMPM.contentEquals("PM")) { // Checking to see if ready_at String has a PM at the end
convert_ready_at_HOUR_to_military = get_ready_at_HOUR + 12; // If it does, add 12 so we can get military time
}
if (get_ready_at_HOUR == 12 & get_ready_at_AMPM.contentEquals("PM") ) {// If hour is set at 12 PM, leave it at 12
convert_ready_at_HOUR_to_military = 12;
}
if (get_ready_at_AMPM.matches("AM")) { // Do Nothing if its AM
convert_ready_at_HOUR_to_military = get_ready_at_HOUR + 0;
}
/* ############################################################
* #### GET THE CURRENT DATE/TIME FROM USERS DEVICE ###
* ############################################################
*/
int current_MONTH = Calendar.getInstance().get(Calendar.MONTH); // Get todays month
int current_DAY = Calendar.getInstance().get(Calendar.DAY_OF_MONTH); // Get todays date
int current_YEAR = Calendar.getInstance().get(Calendar.YEAR); // Get todays year
int current_HOUR = Calendar.getInstance().get(Calendar.HOUR_OF_DAY); // Get todays Hour in military format
int current_MINUTE = Calendar.getInstance().get(Calendar.MINUTE); // Get todays minute
int current_year_FIXUP = current_YEAR - 1900; // example, this year is 2013, subtract 1900 you get 113 which is what Date parameter is requesting
int get_ready_at_year_FIXUP = get_ready_at_YEAR - 1900;
int get_ready_at_MONTH_FIXUP = // * We dont need to fixup current_MONTH because java has already done so
get_ready_at_MONTH - 1; // <-- we subtract 1 because according to parameters, January starts at 0 and December is 11
/*
* How to use Date(int, int, int)
*
*
* *Parameters*
* - year the year, 0 is 1900.
* - month the month, 0 - 11.
* - day the day of the month, 1 - 31
*/
Date ready_at_time = new Date(get_ready_at_year_FIXUP, get_ready_at_MONTH_FIXUP, get_ready_at_DAY); // (2010, June, 20th) = (110, 5, 20) June is 5 instead of 6 because we start
Date current_time = new Date(current_year_FIXUP, current_MONTH, current_DAY); // January at 0 in Java. As for days, it starts at 1 like normal.
int days_between = Days.daysBetween(new DateTime(current_time), new DateTime(ready_at_time)).getDays(); // Get the difference in days of current date and ready_at date
int minutes_difference_in_days_between = days_between * 1440; // 1440 minutes = 1 day. multiply with the date difference of int days_between to get the minutes between those days.
/* ############################################################################
* #### FINAL OUTPUT OF CALCULATING TIMES FROM CURRENT AND READY_AT ###
* ############################################################################
*/
int current_TOTAL_MINUTES = current_HOUR * 60 + current_MINUTE; // Multiply hour by 60 to get the minutes in the hour = RIGHT NOW'S time in minute format
int ready_at_TOTAL_MINUTES = convert_ready_at_HOUR_to_military * 60 + get_ready_at_MINUTE + minutes_difference_in_days_between;
PU_time_until_late = ready_at_TOTAL_MINUTES - current_TOTAL_MINUTES;
LOGCAT.DEBUG("READY_AT " + tracking_Number,"'" + days_between + "'" + " days between today and when the package is scheduled for pickup");
LOGCAT.DEBUG("READY_AT " + tracking_Number, "ready_at String's time = " + parse_ready_at_ARRAY +
" Time now in Minutes = " + current_TOTAL_MINUTES +
" ready_at time in Minutes = " + ready_at_TOTAL_MINUTES +
" Minutes left to complete pickup = " + PU_time_until_late + "\n" + "\n" + "\n" + " ");
}
}
catch (Exception e) {
LOGCAT.DEBUG("Jobs", "Error Splitting/Converting ready_at Time");
}
}
public void GetWindowParameters() {
WindowManager.LayoutParams params = getWindow().getAttributes();
Jobs.this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Jobs.this.getWindow().setBackgroundDrawableResource(R.drawable.listviewbackground);
/* params.x = 0;
params.height = 480;
params.width = 480;
params.y = 160; */
params.y = 160;
params.height = 600;
params.dimAmount = .70f;
Jobs.this.getWindow().setAttributes(params);
}
時差を取得するコードは次のとおりです **public void parse_ready_at()***警告* これを行うためのはるかに簡単な方法があると確信していますが、私は初心者で、それ以上のことは知りません笑
public void parse_ready_at(JSONObject JobsArray, int i1) {
String parse_ready_at_ARRAY;
String tracking_Number;
try { for (i1 = 0; i1 < JOBS.length(); i1++) {
JobsArray = JOBS.getJSONObject(i1);
parse_ready_at_ARRAY = JobsArray.getString(ready_at);
tracking_Number = JobsArray.getString(job);
//Example of ready_at String --> "ready_at": "07/25/2012 08:26:00 PM" we split time from date to get time only
SimpleDateFormat parserSDF = new SimpleDateFormat("M/d/yyyy hh:mm:ss a"); // <--- Correct format to read "lastLatitudeUpdate"
try { parserSDF.parse(parse_ready_at_ARRAY);
}
catch (ParseException e) { LOGCAT.DEBUG("JOBS parse_ready_at", "Error parsing tracking number = " + tracking_Number + " ready_at Array = " + parse_ready_at_ARRAY + " Error = " + e.toString()); }
/* ################################################
* #### DISSECTING READY_AT STRING ARRAY ###
* ################################################
*/
String[] dissect_ready_at_DATE_TIME = parse_ready_at_ARRAY.split(" "); // Splitting space between 07/25/2012 and 08:26:00 and PM
String get_ready_at_DATE = String.valueOf(dissect_ready_at_DATE_TIME[0]); // Set at 0 because we want date. 07-25-2012
String get_ready_at_TIME = String.valueOf(dissect_ready_at_DATE_TIME[1]); // Set at 1 because we want time. 08:26:00
String get_ready_at_AMPM = String.valueOf(dissect_ready_at_DATE_TIME[2]); // Set at 2 because we want AM PM.
/* ########################################################
* #### GETTING DATE FROM READY_AT STRING ARRAY ###
* ########################################################
*/
String[] dissect_ready_at_DATE = get_ready_at_DATE.split("/"); // Splitting the / between 07 and 25 and 2012 from 07/25/2012
int get_ready_at_MONTH = Integer.valueOf(dissect_ready_at_DATE[0]); // Set at 0 because we want month. 07
int get_ready_at_DAY = Integer.valueOf(dissect_ready_at_DATE[1]); // Set at 1 because we want day. 25
int get_ready_at_YEAR = Integer.valueOf(dissect_ready_at_DATE[2]); // Set at 2 because we want yeay. 2012
/* ########################################################
* #### GETTING TIME FROM READY_AT STRING ARRAY ###
* ########################################################
*/
String[] dissect_ready_at_TIME = get_ready_at_TIME.split(":"); // Splitting the : between 08 and 26 and 00
int get_ready_at_HOUR = Integer.valueOf(dissect_ready_at_TIME[0]); // Set at 0 because we want hour. 08
int get_ready_at_MINUTE = Integer.valueOf(dissect_ready_at_TIME[1]); // Set at 1 because we want minute. 26
/* ################################################################
* #### CONVERT HOUR FROM READY_AT STRING TO MILITARY TIME ###
* ################################################################
*/
int convert_ready_at_HOUR_to_military = 0; // By default, ready_at String is in 12 hour format. we need to fix it so it is military time.
if (get_ready_at_AMPM.contentEquals("PM")) { // Checking to see if ready_at String has a PM at the end
convert_ready_at_HOUR_to_military = get_ready_at_HOUR + 12; // If it does, add 12 so we can get military time
}
if (get_ready_at_HOUR == 12 & get_ready_at_AMPM.contentEquals("PM") ) {// If hour is set at 12 PM, leave it at 12
convert_ready_at_HOUR_to_military = 12;
}
if (get_ready_at_AMPM.matches("AM")) { // Do Nothing if its AM
convert_ready_at_HOUR_to_military = get_ready_at_HOUR + 0;
}
/* ############################################################
* #### GET THE CURRENT DATE/TIME FROM USERS DEVICE ###
* ############################################################
*/
int current_MONTH = Calendar.getInstance().get(Calendar.MONTH); // Get todays month
int current_DAY = Calendar.getInstance().get(Calendar.DAY_OF_MONTH); // Get todays date
int current_YEAR = Calendar.getInstance().get(Calendar.YEAR); // Get todays year
int current_HOUR = Calendar.getInstance().get(Calendar.HOUR_OF_DAY); // Get todays Hour in military format
int current_MINUTE = Calendar.getInstance().get(Calendar.MINUTE); // Get todays minute
int current_year_FIXUP = current_YEAR - 1900; // example, this year is 2013, subtract 1900 you get 113 which is what Date parameter is requesting
int get_ready_at_year_FIXUP = get_ready_at_YEAR - 1900;
int get_ready_at_MONTH_FIXUP = // * We dont need to fixup current_MONTH because java has already done so
get_ready_at_MONTH - 1; // <-- we subtract 1 because according to parameters, January starts at 0 and December is 11
/*
* How to use Date(int, int, int)
*
*
* *Parameters*
* - year the year, 0 is 1900.
* - month the month, 0 - 11.
* - day the day of the month, 1 - 31
*/
Date ready_at_time = new Date(get_ready_at_year_FIXUP, get_ready_at_MONTH_FIXUP, get_ready_at_DAY); // (2010, June, 20th) = (110, 5, 20) June is 5 instead of 6 because we start
Date current_time = new Date(current_year_FIXUP, current_MONTH, current_DAY); // January at 0 in Java. As for days, it starts at 1 like normal.
int days_between = Days.daysBetween(new DateTime(current_time), new DateTime(ready_at_time)).getDays(); // Get the difference in days of current date and ready_at date
int minutes_difference_in_days_between = days_between * 1440; // 1440 minutes = 1 day. multiply with the date difference of int days_between to get the minutes between those days.
/* ############################################################################
* #### FINAL OUTPUT OF CALCULATING TIMES FROM CURRENT AND READY_AT ###
* ############################################################################
*/
int current_TOTAL_MINUTES = current_HOUR * 60 + current_MINUTE; // Multiply hour by 60 to get the minutes in the hour = RIGHT NOW'S time in minute format
int ready_at_TOTAL_MINUTES = convert_ready_at_HOUR_to_military * 60 + get_ready_at_MINUTE + minutes_difference_in_days_between;
PU_time_until_late = ready_at_TOTAL_MINUTES - current_TOTAL_MINUTES;
LOGCAT.DEBUG("READY_AT " + tracking_Number,"'" + days_between + "'" + " days between today and when the package is scheduled for pickup");
LOGCAT.DEBUG("READY_AT " + tracking_Number, "ready_at String's time = " + parse_ready_at_ARRAY +
" Time now in Minutes = " + current_TOTAL_MINUTES +
" ready_at time in Minutes = " + ready_at_TOTAL_MINUTES +
" Minutes left to complete pickup = " + PU_time_until_late + "\n" + "\n" + "\n" + " ");
}
}
catch (Exception e) {
LOGCAT.DEBUG("Jobs", "Error Splitting/Converting ready_at Time");
}
}
基本的に、私はこれらのライトをオンタイム、レイトなどに似せたいと思っています...黄金のスプーンで餌を与えられているようには見えません.正しい方向へのいくつかのガイダンスだけです. ありがとう!
ここで編集するのは、含めるのを忘れていた私のスタック トレースです。onPostExecute でヌル ポインター例外が発生しています。私は textView を宣言し、すでに onCreate で定義しています。ListAdapter でも既に適切に呼び出していると思います。
if ( PU_ time_until_late < 60) { imageViewReadyAt.setBackgroundColor (R.drawable.green_light); }
編集さて、imageViewReadyAt Textviewを呼び出すときにnull例外が発生する理由がわかりました。そのテキストビューは、カスタム list_item に属する別の XML に属しています。それでも、いくつかの助けを借りることができます。