使用此语句,插入列表视图,
public View getView(int pos, View child, ViewGroup parent) {
SharedPreferences prefs = mContext.getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
String anno = prefs.getString(ANNO, "2014");
String malattia = prefs.getString(MUTUA, "M");
String ferie_1 = prefs.getString(FERIE, "F");
String par_1 = prefs.getString(PAR, "PAR");
Holder mHolder;
LayoutInflater layoutInflater;
//text in which you want to find "M"
if (child == null) {
layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutInflater.inflate(R.layout.row, null);
mHolder = new Holder();
mHolder.txt_notes = (TextView) child.findViewById(R.id.txt_notes);
mHolder.txt_mese = (TextView) child.findViewById(R.id.txt_mese);
mHolder.txt_giorni = (TextView) child.findViewById(R.id.txt_giorni);
mHolder.txt_turno = (TextView) child.findViewById(R.id.txt_turno);
mHolder.txt_ore = (TextView) child.findViewById(R.id.txt_ore);
mHolder.txt_id = (TextView) child.findViewById(R.id.txt_id);
child.setTag(mHolder);
} else {
mHolder = (Holder) child.getTag();
}
mHolder.txt_giorni.setText(giorniName.get(pos));
mHolder.txt_turno.setText(turnoName.get(pos));
mHolder.txt_ore.setText(oreName.get(pos));
mHolder.txt_id.setText(dataName.get(pos));
mHolder.txt_giorni.setText(giorniName.get(pos));
mHolder.txt_turno.setText(turnoName.get(pos));
mHolder.txt_ore.setText(oreName.get(pos));
mHolder.txt_id.setText(dataName.get(pos));
String Turni = turnoName.get(pos);
String Mese = meseName.get(pos);
String Data = dataName.get(pos);
String Giorni = giorniName.get(pos);
String Notes = notesName.get(pos);
if (Turni != null)
if (Turni.equals(""+malattia)){
mHolder.txt_turno.setTextColor(Color.RED);
mHolder.txt_ore.setTextColor(Color.RED);
} else
if (Turni.equals(""+par_1)){
mHolder.txt_turno.setTextColor(Color.GREEN);
mHolder.txt_ore.setTextColor(Color.GREEN);
} else
if (Turni.equals(""+ferie_1)){
mHolder.txt_turno.setTextColor(Color.YELLOW);
mHolder.txt_ore.setTextColor(Color.YELLOW);
} else
mHolder.txt_turno.setTextColor(Color.WHITE);
if (Notes != null) {
if (!Notes.equalsIgnoreCase("")){
mHolder.txt_notes.setBackgroundColor(Color.GREEN);
mHolder.txt_notes.setTextColor(Color.BLUE);
mHolder.txt_notes.setText("Nota");
} else {
mHolder.txt_notes.setBackgroundColor(Color.RED);
}
}
if (Giorni != null)
if (Giorni.equals("Sab")){
mHolder.txt_giorni.setBackgroundColor(Color.parseColor("#FFA500"));
} else
if (Giorni.equals("Dom")
{
mHolder.txt_giorni.setBackgroundColor(Color.RED);
} else
mHolder.txt_giorni.setBackgroundColor(Color.BLACK);
if (Giorni != null)
if (Giorni.equals("Sab")){
mHolder.txt_id.setBackgroundColor(Color.parseColor("#FFA500"));
} else
if (Giorni.equals("Dom"){
mHolder.txt_id.setBackgroundColor(Color.RED);
} else
mHolder.txt_id.setBackgroundColor(Color.BLACK);
return child;
}
public class Holder {
TextView txt_notes;
TextView txt_mese;
TextView txt_giorni;
TextView txt_turno;
TextView txt_ore;
TextView txt_id;
}
}
如果数据库包含一些数据,我应该使用绿色背景的texview txt_notes
,而如果它是空的,它应该是红色的。
我的问题是,如果我滚动listview
,逐个所有TextView
转绿,如果我刷新整个班级回到正确的位置。
我该如何解决这个问题?
答案 0 :(得分:0)
Holder
和setTag
,getTag
似乎很好!因此,可能导致错误回收的唯一原因是数据的null
值。
因此,如果其中一个变为null
String Turni = turnoName.get(pos);
String Mese = meseName.get(pos);
String Data = dataName.get(pos);
String Giorni = giorniName.get(pos);
String Notes = notesName.get(pos);
然后没有为setTextColor
调用setBackgroundColor
或TextView
,因此它保留了最后一次从Holder
回收的信息(不是XML)。为了避免这种情况,你应该"硬编码"在所有情况下的颜色。
if (Notes != null) {
if (!Notes.equalsIgnoreCase("")){
mHolder.txt_notes.setBackgroundColor(Color.GREEN);
mHolder.txt_notes.setTextColor(Color.BLUE);
mHolder.txt_notes.setText("Nota");
} else {
mHolder.txt_notes.setBackgroundColor(Color.RED);
}
} else {
mHolder.txt_notes.setBackgroundColor(your_default_color_here);
}
如果处理null
和empty
的方式相同,则可以使用TextUtils.isEmpty(Notes)
在两种情况下都返回true。