大家好,我有3列Sqlite数据库放入ListView中。如果给定的日期早于当前日期,我试图将员工DOB(TextView)的empDobTxt颜色更改为红色。

我正在关注this tutorial

   public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();

        holder.empIdTxt = (TextView) convertView
                .findViewById(R.id.txt_emp_id);
        holder.empNameTxt = (TextView) convertView
                .findViewById(R.id.txt_emp_name);
        holder.empDobTxt = (TextView) convertView
                .findViewById(R.id.txt_emp_dob);
        holder.empSalaryTxt = (TextView) convertView
                .findViewById(R.id.txt_emp_salary);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    Employee employee = (Employee) getItem(position);
    holder.empIdTxt.setText(employee.getId() + "");
    holder.empNameTxt.setText(employee.getName());
    holder.empSalaryTxt.setText(employee.getSalary() + "");
    holder.empDobTxt.setText(formatter.format(employee.getDateOfBirth()));

    String dtStart = empDobTxt.getText().toString();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
    String currentDateandTime = sdf.format(new Date());
    try {
        Date date = sdf.parse(dtStart);
        Date date1 = sdf.parse(currentDateandTime);
        if(date.before(date1));
        {
            empDobTxt.setTextColor(Color.RED);
        }
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return convertView;

}


这是logCat

 02-17 11:33:00.486: E/AndroidRuntime(7308):        java.lang.NullPointerException
 02-17 11:33:00.486: E/AndroidRuntime(7308):    at  com.androidopentutorials.sqlite.adapter.EmpListAdapter.getView(EmpListAdapter.java:88)

最佳答案

尝试使用String dtStart = holder.empDobTxt.getText().toString();代替String dtStart = empDobTxt.getText().toString();

也是holder.empDobTxt.setTextColor(Color.RED);而不是empDobTxt.setTextColor(Color.RED);

您在此字符串处也有多余的分号

if(date.before(date1));

这就是您所有文本均为红色的原因。

10-08 07:16