最近给公司做了一个查看资讯的应用,参考别的应用,要加上(已读过的信息要用别的颜色进行区分)的需要.
下面是实现的部分代码:
继承自BaseAdapter
public View getView(int position, View convertView, ViewGroup parent) {
// 利用ViewHolder来保存与convertView相关联的子View,避免调用 findViewById方法,以提高效率
ViewHolder viewHolder = null;
// 重复利用 convertView,convertView是旧的View,建议先判断是否为空,
// 如果不为空,可以修改其内容来显示新的row
if (convertView == null) {
// Log.d("MyBaseAdapter", "新建convertView,position="+position);
convertView = mInflater.inflate(myresource, null);
viewHolder = new ViewHolder();
viewHolder.txtViewTitle = (TextView) convertView
.findViewById(myto[0]);
viewHolder.txtViewDateTime = (TextView) convertView
.findViewById(myto[1]);
viewHolder.txtViewNewsId = (TextView) convertView
.findViewById(myto[2]);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
// Log.d("MyBaseAdapter", "旧的convertView,position="+position);
}
Class cla = null;
Field field = null;
String value = null;
TextView tv = null;
Info info = (Info) mylist.get(position);
boolean isRead = false;
m_MyDataBaseAdapter.open();
isRead = m_MyDataBaseAdapter.getIsRead(info.getNewsId());//用来判断该信息是否已读过
m_MyDataBaseAdapter.close();
cla = info.getClass();
for (int i = 0; i < myfrom.length; i++) {
try {
field = cla.getDeclaredField(myfrom[i]);
field.setAccessible(true);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
try {
value = (String) field.get(info);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
tv = (TextView) convertView.findViewById(myto[i]);
tv.setText(value);
// 利用ViewHolder来保存与convertView相关联的子View,避免调用 findViewById方法,以提高效率
ViewHolder viewHolder = null;
// 重复利用 convertView,convertView是旧的View,建议先判断是否为空,
// 如果不为空,可以修改其内容来显示新的row
if (convertView == null) {
// Log.d("MyBaseAdapter", "新建convertView,position="+position);
convertView = mInflater.inflate(myresource, null);
viewHolder = new ViewHolder();
viewHolder.txtViewTitle = (TextView) convertView
.findViewById(myto[0]);
viewHolder.txtViewDateTime = (TextView) convertView
.findViewById(myto[1]);
viewHolder.txtViewNewsId = (TextView) convertView
.findViewById(myto[2]);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
// Log.d("MyBaseAdapter", "旧的convertView,position="+position);
}
Class cla = null;
Field field = null;
String value = null;
TextView tv = null;
Info info = (Info) mylist.get(position);
boolean isRead = false;
m_MyDataBaseAdapter.open();
isRead = m_MyDataBaseAdapter.getIsRead(info.getNewsId());//用来判断该信息是否已读过
m_MyDataBaseAdapter.close();
cla = info.getClass();
for (int i = 0; i < myfrom.length; i++) {
try {
field = cla.getDeclaredField(myfrom[i]);
field.setAccessible(true);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
try {
value = (String) field.get(info);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
tv = (TextView) convertView.findViewById(myto[i]);
tv.setText(value);
if (isRead == true && i == 0) { //true 标明该信息已读过
tv.setTextColor(myContext.getResources().getColor(R.color.isRead));
} else if (isRead == false && i == 0) { //false 标明该信息未读过
//tv.setTextColor(myContext.getResources().getColor(R.color.isUnRead));
tv.setTextColor(android.graphics.Color.BLACK);
}
}
return convertView;
}
从详细内容查看页返回时,要刷新ListView
/**
* 从其它activity返回时,刷新ListView
*/
@Override
protected void onRestart() {
super.onRestart();
myadapter.notifyDataSetChanged();//主要是为了刷新最后一次看信息的颜色
}
* 从其它activity返回时,刷新ListView
*/
@Override
protected void onRestart() {
super.onRestart();
myadapter.notifyDataSetChanged();//主要是为了刷新最后一次看信息的颜色
}
简单上个图:前两条是已经看过的,后两条是未看过的.