获取当前日期之前的日期更改文本视图颜色
Get Date before current date change textview color
大家好,我已将 3 列 Sqlite 数据库放入 ListView
。如果给定的日期早于当前日期,我正在尝试将员工 DOB (empDobTxt
) 的 TextView
颜色更改为红色。
我正在关注 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));
这就是为什么你所有的文字都是红色的原因。
试试这个。
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();
}
大家好,我已将 3 列 Sqlite 数据库放入 ListView
。如果给定的日期早于当前日期,我正在尝试将员工 DOB (empDobTxt
) 的 TextView
颜色更改为红色。
我正在关注 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));
这就是为什么你所有的文字都是红色的原因。
试试这个。
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();
}