ListView根本没有出现

时间:2013-06-06 17:20:44

标签: android android-listview

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

Start.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));
            }
    }
}

start.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>

1 个答案:

答案 0 :(得分:2)

我看到的第一个问题出现在您的第一个嵌套LinearLayout

android:layout_height="fill_parent"

尝试将其更改为

android:layout_height="wrap_content"

你没有其他任何空间,因为你告诉它要占用所有height

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