2013-06-06 9 views
0

В настоящее время я изучаю разработку Android, и я пытаюсь заставить ListView работать. Однако ListView даже не отображается на графическом макете и эмуляторе. Eclipse не обнаруживает ошибок с моим кодом, и я не уверен, почему он вообще не появляется. Ниже приведены файлы классов java (Start.java) и два xml-файла (start.xml & member_names_inflate.xml).ListView вообще не отображается

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> 

ответ

2

Первый вопрос, который я вижу в вашей первой вложенной LinearLayout

android:layout_height="fill_parent" 

попытаться изменить что

android:layout_height="wrap_content" 

Вам нет места для чего-нибудь еще, потому что вы говорите он возьмет на себя все height.

Также обратите внимание, что fill_parent устарел, и вместо этого вы должны использовать match_parent. Это не проблема, но может быть и в будущем. LinearLayout по умолчанию имеет horizontal orientation, поэтому вам не нужно объявлять об этом, но это не помешает сделать это ... просто FYI на этом

+0

Я только что проверил это, и оно появляется в редакторе с этими изменениями. – codeMagic

Смежные вопросы