2

任何人都可以就我应该如何正确处理这个问题给我任何建议吗?

问题来了,先喝杯啤酒。我试图解释可能需要一个冷的:)

  • 我有一个通过 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()***WARNING* 我很确定有一个更简单的方法可以做到这一点,但是,我是新手,不知道更好的哈哈

    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 时会出现空异常。该文本视图属于另一个属于自定义 list_item 的 XML。不过仍然可以使用一些帮助。

4

2 回答 2

0

我不确定这是一个错误还是什么,但我也遇到了从onPostExecute(). 为了解决这个问题,我要么从那里调用一个方法到主要活动并让它与视图一起工作,要么使用处理程序并将消息发布到使用 UI 线程的正确活动。

为了确保我在 UI 线程上,我通常使用处理程序消息,如下所示:

Handler handlerJobs = new jobsHandler();

public class jobsHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
        switch(msg.arg1) {
            case 1:
                updateBackgroundColor(msg.arg2);
                break;

            default:
                super.handleMessage(msg);
        }
    }
}

private void updateBackgroundColor(int dataPassedInMessage) {
    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);

    }
}

然后调用它你会使用:

@Override
protected void onPostExecute(Void result) {
    Message updateBackground = new Message();
    updateBackground.arg1 = 1;
    updateBackground.arg2 = R.drawable.green_light;  //or any int you need to pass
    handlerJobs.sendMessage(updateBackground);
}

编辑:更新了示例,因此它应该与您的代码一起使用

于 2013-08-12T01:29:42.490 回答
0

我认为问题是您正在设置背景颜色而不是设置背景图像。您的图像设置代码应如下所示:

imageViewReadyAt.setBackgroundDrawable(R.drawable.green_light);
于 2013-08-05T05:08:56.453 回答