0

我目前正在学习 android 开发,并且正在尝试让 ListView 工作。但是,ListView 甚至没有显示在图形布局和模拟器上。Eclipse 也没有发现我的代码有任何错误,我不确定为什么它根本没有出现。下面是 java 类文件 (Start.java) 和两个 xml 文件 (start.xml & member_names_inflate.xml)。

启动.java

public class Start extends ListActivity implements OnClickListener
{
    private static EditText nameText = null;
    private static Button nameAdd = null;
    private static final Vector<String> nameContent = new Vector<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.start);
            nameContent.add("Steve");
            nameContent.add("Larry");

            nameText = (EditText)findViewById(R.id.name);
            nameAdd = (Button)findViewById(R.id.add);
            nameAdd.setOnClickListener(this);
            setListAdapter(new ListViewAdapter(this));
    }

    private static class ListViewAdapter extends BaseAdapter
    {
            private LayoutInflater nameInflater;

            public ListViewAdapter(Context context)
            {
                    nameInflater = LayoutInflater.from(context);
            }

            public int getCount()
            {
                    return nameContent.size();
            }

            public Object getItem(int position)
            {
                    return position;
            }

            public long getItemId(int position)
            {
                    return position;
            }

            public View getView(int position, View view, ViewGroup group)
            {
                    ListContent contents;

                    if(view == null)
                    {
                            view = nameInflater.inflate(R.layout.member_names_inflate, null);
                            contents = new ListContent();
                            contents.text = (EditText)view.findViewById(R.id.name_first);
                            contents.text.setCompoundDrawables(view.getResources().getDrawable(R.drawable.bullet), null, null, null);
                            view.setTag(contents);
                    }
                    else
                    {
                            contents = (ListContent)view.getTag();
                    }

                    contents.text.setText(nameContent.get(position));
                    return view;
            }

            static class ListContent
            {
                    TextView text;
            }
    }

    public void onClick(View v)
    {
            if(v == nameAdd)
            {
                    nameContent.add(nameText.getText().toString());
                    setListAdapter(new ListViewAdapter(this));
            }
    }
}

开始.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/splash_bg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:padding="10dp"
android:orientation="vertical" >

<TextView
    android:id="@+id/subtitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textSize="20sp"
    android:paddingBottom="10dp"
    android:textColor="@color/splash_text"
    android:text="@string/sub" />

<EditText 
    android:id="@+id/grp_name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/field1"
    android:lines="1" />

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/members"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:paddingTop="5dp"
        android:textColor="@color/splash_text"
        android:text="@string/members" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="@string/field2" />

        <Button
            android:id="@+id/add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/add" />

    </LinearLayout>

    <ListView
        android:id="@android:id/list"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent" />

</LinearLayout>

member_names_inflate.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/inflate_root"
android:background="@color/splash_bg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<LinearLayout 
    android:id="@+id/inflate_sub"
    android:layout_height="wrap_content"
    android:paddingLeft="5dp"
    android:layout_width="fill_parent" >

    <TextView
        android:id="@+id/name_bullet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/bullet" />"

    <EditText
        android:id="@+id/name_first"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="@color/splash_text"
        android:inputType="textCapWords"
        android:text="@+id/name_first" />

</LinearLayout>

4

1 回答 1

2

我看到的第一个问题是在你的第一个嵌套LinearLayout

android:layout_height="fill_parent"

尝试将其更改为

android:layout_height="wrap_content"

您没有空间容纳其他任何东西,因为您告诉它要占用所有height.

另请注意,fill_parent它已被弃用,您应该match_parent改用它。这不是问题,但将来可能会出现。LinearLayout默认情况下有一个horizontal orientation,所以你不需要声明它,但这样做并没有什么坏处......只是一个仅供参考

于 2013-06-06T17:23:26.920 回答