0

I have an AlertDialog that I inflate from this layout:

<?xml version="1.0" encoding="utf-8"?>    
    <ListView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/List"
        android:layout_alignParentTop="true"
        android:layout_width="match_parent"
        android:layout_height="400dp"/>

I need every item of the list to be a view described by this xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#20f0f0f0"
    android:orientation="horizontal" >

    <CheckBox 
        android:layout_gravity="center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:id="@+id/cb_persistent"/>

    <TextView
        style="@style/Label"
        android:layout_gravity="center_vertical"
        android:gravity="center_horizontal|center_vertical"
        android:layout_width="fill_parent"
        android:layout_height="@dimen/button_height"
        android:layout_toLeftOf="@+id/btn_connect"
        android:layout_toRightOf="@+id/cb_persistent"
        android:id="@+id/lbl_name_address"/>

    <Button
        android:layout_gravity="center_vertical" 
        style="@style/Button.Plain"
        android:layout_width="wrap_content"
        android:layout_alignParentRight="true"
        android:id="@+id/btn_connect"
        android:text="@string/Connect"/>
</RelativeLayout>

And this is the adapter I'm trying to use for it. I've also tried implementing ListAdapter, result was the same: only 1 list row is showing, the dialog is exactly 1 row high. With this adapter it's the last row, with ListAdapter - the first. What am I doing wrong?

private class ListItem extends View {

        public ListItem(Context context) {
            super(context);
            View content = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.list_element, null);
            setView(content);

            m_cbPersistent = (CheckBox) content.findViewById(R.id.cb_persistent);
            m_btnConnect   = (Button)   content.findViewById(R.id.btn_connect);
            m_lblName      = (TextView) content.findViewById(R.id.lbl_name_address);
        }

        public CheckBox m_cbPersistent = null;
        public Button   m_btnConnect   = null;
        public TextView m_lblName      = null;
    }

class NewAdapter extends ArrayAdapter<ListItem> 
    {

        public NewAdapter(Context context) {
            super(context, 0);
            m_context = context;
        }

        @Override
        public int getCount() {
            return m_items.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (position < getCount())
                return m_items.elementAt(position);
            else
                return null;
        }

        void addNewItem ()
        {
            m_items.add(new NetworkCameraEntry(m_context));
        }

        void removeItem (int index)
        {
            if (index < getCount())
            {
                m_items.remove(index);
            }
        }

        @Override
        public boolean isEmpty() {
            return m_items.size() <= 0;
        }

        @Override
        public boolean areAllItemsEnabled() {
            return true;
        }

        @Override
        public boolean isEnabled(int position) {
            return true;
        }

            private Context m_context = null;

        private Vector<ListItem> m_items = new Vector<ListItem>();
    }

This is how I initialize it in the AlertDialog's constructor:

public class MyDialog extends AlertDialog {

public MyDialog(Context context) {
        super(context, 0);

        View content = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.network_cameras_list_window, null);
        setView(content);

        m_adapter = new NewAdapter(context);
        m_list = (ListView) content.findViewById(R.id.List);
        m_list.setAdapter(m_adapter);   

        m_adapter.addNewItem();

        m_adapter.addNewItem();
    }

    private ListView m_list = null;
    private NewAdapter m_adapter = null;
}
4

1 回答 1

1

m_items.size is 1 when the adapter is constructed and gets populated over time. m_items.size is cached so you have to invalidate the adapter on each m_items.add

Yet this is not the way to go. A better option is to get your data populated before constructing the adapter and pass is to the adapter. Any altering of the data you have to notify / invalidate the adapter with

notifyDataSetInvalidated();
notifyDataSetChanged();
于 2012-09-18T09:12:50.623 回答