Reputation: 33
I have a ListActivity with a custom row layout. The row layout consists of a checkbox, a textview and an imagebutton.
Behind this is an ArrayList of a custom object containing simply a String and a boolean. I want to checkbox to reflect the boolean, the textview to reflect the Sting, and the button on the right will kick off an action.
I use the last constructor listed on the ArrayAdapter page to try to bind this all together. ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)
For some reason the textView is populated by the objects toString(), and not by getView. Therefore the checkbox never gets the boolean value at all. Any ideas?
public class ListSelection extends ListActivity {
private ArrayList<ListItem> list;
private ArrayList<String> deleted;
static private final String TAG = "kinder";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "ListSelection.onCreate");
list = new ArrayList<ListItem>();
deleted = new ArrayList<String>();
Intent temp = getIntent();
String[] listNames = temp.getStringArrayExtra("listNames");
boolean[] listStatus = temp.getBooleanArrayExtra("listStatus");
for (int i = 0; i < listNames.length; i++) {
list.add(new ListItem(listNames[i], listStatus[i]));
}
**setListAdapter(new ArrayAdapter<ListItem>(this, R.layout.selectionlistitem, R.id.text, list));**
}
private class ListItem {
String listName;
Boolean listStatus;
public ListItem(String name, boolean status) {
listName = name;
listStatus = status;
}
@Override
public String toString() {
return listName;
}
}
public class ListItemArrayAdapter extends ArrayAdapter<ListItem> {
int resource;
LayoutInflater vi;
private List<ListItem> items;
public ListItemArrayAdapter(Context context, int _resource, List<ListItem> listitems) {
super(context, _resource, listitems);
Log.d(TAG, "listItemArrayAdapter");
vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
resource = _resource;
this.items = listitems;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d(TAG, "listItemArrayAdapter.getView");
LinearLayout newView;
ListItem item = getItem(position);
if (convertView == null) {
newView = new LinearLayout(getContext());
vi.inflate(resource, newView);
} else {
newView = (LinearLayout)convertView;
}
TextView textview = (TextView)newView.findViewById(R.id.text);
Log.d(TAG, item.listName);
textview.setText(item.listName);
CheckBox checkbox = (CheckBox)newView.findViewById(R.id.checkbox);
checkbox.setChecked(item.listStatus);
return newView;
}
}
}
Upvotes: 3
Views: 2705
Reputation: 234797
It seems to me that you should replace this line:
setListAdapter(new ArrayAdapter<ListItem>(this, R.layout.selectionlistitem, R.id.text, list));
with this:
setListAdapter(new ListItemArrayAdapter<ListItem>(this, R.layout.selectionlistitem, list));
As it stands, you are using the default view generation logic of ArrayAdapter
, which is to feed toString()
for each array element to a TextView
.
Upvotes: 4