Android Listfragment Individually Change Row Background Color
Solution 1:
Your current getView
implementation should be moved into a ListAdapter
implementation instead of your TrackerFragment
class. Since you're using ArrayAdapter
, you can subclass that and put the code in there. ArrayAdapter
already implements getView
, but you'll override it to provide your specialized behavior.
The reason you're getting a NullPointerException is because you're calling getView
and passing in the list view, which does not have a tag associated with it -- so holder = (ViewHolder) vi.getTag();
assigns null
to holder
. That said, you shouldn't be calling getView
directly. The system will call that for you whenever it needs to display a list item. When the system calls the getView
method, it initially passes in null
to have the views created, and every call where convertView
is not null
is a view created by that method.
Solution 2:
Looks like the same problem as the post you linked: the getView() method isn't nested inside the class.
Or your code doesn't show anything that would call it either.
Solution 3:
The more I look over this, the more I wonder about the basic premise you are using. I think you're making it overly complicated. I would do it like this:
public View getView(View convertView)
{
Viewvi= convertView;
TextViewviText=null;
if (vi == null)
vi = LayoutInflater.from(getActivity()).inflate(R.layout.list_item, null);
viText = (TextView)vi.findViewById(R.id.line);
if (viText == null) return vi;
StringviString= viText.getText().toString();
if (viString.contains("OUT OF LOCK"))
{
viText.setBackgroundResource(R.color.red);
}
elseif (viString.contains("IN LOCK"))
{
viText.setBackgroundResource(R.color.green);
}
return vi;
}
I don't think you are using the holder
in the way you think... the loop you have in there will just loop through setting the background resource to whatever the last trigger to set the backgroundResource is, no matter what.
If I have missed the point in this, let me know. But, my basic thought would be to remove as much complexity as you can until it works, and if you've had to remove something important, slowly add it back in.
Post a Comment for "Android Listfragment Individually Change Row Background Color"