具有自定义布局的ListActivity不会调用getview

时间:2011-02-14 02:34:13

标签: android listactivity android-arrayadapter

我有一个带有自定义行布局的ListActivity。行布局包含一个复选框,一个textview和一个图像按钮。

这是一个自定义对象的ArrayList,只包含一个String和一个布尔值。我想复选框以反映布尔值,文本视图反映Sting,右侧的按钮将启动操作。

我使用ArrayAdapter页面上列出的最后一个构造函数来尝试将它们绑定在一起。 ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)

由于某种原因,textView由对象toString()填充,而不是由getView填充。因此,复选框永远不会获得布尔值。有什么想法吗?

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;
        }
    }

}

1 个答案:

答案 0 :(得分:4)

在我看来,你应该替换这一行:

setListAdapter(new ArrayAdapter<ListItem>(this, R.layout.selectionlistitem, R.id.text, list));

用这个:

setListAdapter(new ListItemArrayAdapter<ListItem>(this, R.layout.selectionlistitem, list));

目前,您使用ArrayAdapter的默认视图生成逻辑,即将每个数组元素的toString()提供给TextView