2016-02-17 2 views
1

Привет всем У меня есть 3 столбца базы данных Sqlite в ListView. Я пытаюсь изменить TextView сотрудника DOB (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) 
+0

какая ошибка вы получаете после своего logcat – Madhur

ответ

-2

Попробуйте использовать String dtStart = holder.empDobTxt.getText().toString(); вместо String dtStart = empDobTxt.getText().toString();

Также holder.empDobTxt.setTextColor(Color.RED); вместо empDobTxt.setTextColor(Color.RED);

У вас также есть дополнительная точка с запятой в этой строке

if(date.before(date1));

По этой причине все ваши тексты красные.

+0

благодарит за ваш ответ. он работает, но его всегда красный, моя точка, если его DOB после сегодняшнего дня цвет черный – Riz

+0

Попробуйте удалить дополнительную точку с запятой из этой строки 'if (date.before (date1)); { empDobTxt.setTextColor (Color.RED); } ' –

+0

Ошибка синтаксиса в токене« else », удалить этот токен – Riz

0

Попробуйте это.

String dtStart = empDobTxt.getText().toString(); 
try { 

    Calendar calDtStart = Calendar.getInstance(); 
    SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.EN_US); 
    calDtStart.setTime(dtStart); 

    Calendar nowDate = Calendar.getInstance(); 
    if(calDtStart.before(nowDate)); 
    { 
     holder.empDobTxt.setTextColor(Color.RED); 
    } 
} catch (ParseException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
+0

Метод parse (String) в типе DateFormat не применим для аргументов (Calendar) – Riz

+0

Обновлено с помощью объекта Calendar, проверка PLS – Rakesh