自定义布局的ListActivity不调用getview

发布于 2024-10-17 04:10:30 字数 2952 浏览 6 评论 0原文

我有一个带有自定义行布局的 ListActivity。行布局由复选框、文本视图和图像按钮组成。

其背后是一个自定义对象的 ArrayList,仅包含一个字符串和一个布尔值。我想要复选框来反映布尔值,文本视图来反映 Sting,右侧的按钮将启动一个操作。

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

由于某种原因,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;
        }
    }

}

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏末染殇 2024-10-24 04:10:30

在我看来,您应该将此行: 替换

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

为:

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

就目前而言,您正在使用 ArrayAdapter 的默认视图生成逻辑,即提供 toString() 对于每个数组元素到 TextView

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文