问题
I am using a ListActivity which fills from a database.
Depending on a the information stored in the record I want to format the row in the listView. For example, I have a boolean coulmn which specifies whether the current entry has been 'marked off' and if so then I want to set the strike-through formatting flag on one of the textViews inside the list row.
The way I thought that I could do this was by getting the ListView from the ListActivity and then getting the view that I wanted to change by using the getChildAt() method and then inflating and formatting the view as required. However at this point there are no children in the object and I cannot find another way to acquire the individual views.
I'm trying the following code now but the two text views that I'm trying to inflate come back as null. The code is within an inner class of 'ShoppingList' which is the ListActivity so it's probably something wrong with the way that I'm calling the getView() method.
for (int i = 0; i < adapter.getCount(); i++)
{
View thisView = adapter.getView(i, ShoppingList.this.getListView().getRootView(), ShoppingList.this.getListView());
// inflate views
TextView view_item_name = (TextView) thisView.findViewById(R.id.view_item_name);
TextView view_item_checked = (TextView) thisView.findViewById(R.id.view_item_checked);
}
回答1:
This is what the Adapter is for. :) The getView
method of your adapter (or if you're working with a CursorAdapter
, newView
and bindView
) is where you should format the view for an item based on the data being presented before it is shown in the list.
来源:https://stackoverflow.com/questions/5358270/formatting-listview-content-during-oncreate-method-getting-listview-children