I’ve created a custom ListView adapter/item. The item contains a CheckedTextView and a TextView.
In the adapter’s getView() method, the custom item is created and its setTask(Task) method is called right before it’s returned.
The setTask(Task) method gets 2 Strings and a boolean from the Task object passed in as a parameter. (I do recognize that its address is actually what gets passed.)
One of the Strings is assigned to the custom item’s TextView’s text property IF that String contains text. Otherwise, the TextView’s visible property is set to “gone.”
To make that determination, I check to see if the String’s length is less than or equal to 0… it produces unexpected behavior – it ALWAYS evaluates true.
If I change it to check if the String is equal to “”, it always returns false.
This leads me to believe that the String I’m checking is null. Why would that be?
protected void onFinishInflate() {
super.onFinishInflate();
checkbox = (CheckedTextView)findViewById(android.R.id.text1);
description = (TextView)findViewById(R.id.description);
}
public void setTask(Task t) {
task = t;
checkbox.setText(t.getName());
checkbox.setChecked(t.isComplete());
if (t.getDescription().length() <= 0)
description.setVisibility(GONE);
else
description.setText(t.getDescription());
}
And here’s the getView() method in the adapter:
public View getView(int position, View convertView, ViewGroup parent) {
TaskListItem tli;
if (convertView == null)
tli = (TaskListItem)View.inflate(context, R.layout.task_list_item, null);
else
tli = (TaskListItem)convertView;
tli.setTask(currentTasks.get(position));
return tli;
}
checks the reference of the string object rather than the content. You should use:
which checks the content of the string, rather then the reference. You could use:
but that has a chance of a NullPointerException if myString is null.