gpt4 book ai didi

java - 获取当前日期之前的日期更改 TextView 颜色

转载 作者:行者123 更新时间:2023-12-01 06:23:16 25 4
gpt4 key购买 nike

大家好,我在 ListView 中添加了 3 列 Sqlite 数据库。如果给定的日期早于当前日期,我正在尝试将员工出生日期 (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 = 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();
}

关于java - 获取当前日期之前的日期更改 TextView 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35451370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com