2016-03-08 112 views
0

当我尝试设置listAdapter或适配器(#setListAdapter(), #getListView().setAdapter())它不显示。当我尝试单独设置ListView时,它工作得很好。当我在#runOnUiThread()中运行它时,它不起作用。为什么我的列表不显示?ListActivity - 列表不显示

下面是我的代码

public class ShopActivity extends ListActivity { 

    private Intent mainIntent = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.shop); 
     mainIntent = new Intent(this, MainActivity.class); 

     String[] items = {"Test"}; 
     ArrayAdapter adapter = new ArrayAdapter<>(this, 
     android.R.layout.simple_list_item_1, items); 

     //ListView view = (ListView) findViewById(R.id.list); 

     //view.setAdapter(adapter); 
     getListView().setAdapter(adapter); 
    } 

    public void onBack(View view) { 
     startActivity(mainIntent); 
    } 

} 

编辑:

布局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:weightSum="1"> 

<Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="450dp" 
     android:layout_gravity="center_horizontal" 
     android:layout_centerHorizontal="true" 
     android:textAllCaps="false" 
     android:text="@string/button_back" 
     android:onClick="onBack" 
     android:id="@+id/back" 
/> 

<TextView 
     android:id="@android:id/empty" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="EMPTY" 
     android:layout_centerHorizontal="true" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginTop="-500dp"/> 


<ListView 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.11"/> 

</LinearLayout> 
+0

显示您的布局 –

+0

可以请你列出的XML列表布局。 – Archana

回答

1

将您的linearlayout高度设置为match_parent,并摆脱重量,因为它用于比率,看起来不像您正确使用它。加上-500的余量并不是正确的做法。更换你的LinearLayout先从以下内容:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

如果你希望你的TextView是在顶部的按钮前加入,然后加保证金的按钮来获得保证金文本视图。如果你想要在textview旁边的后退按钮,将它们添加到水平方向的线性布局,并将它们并排放置在顶部。

0

您可能需要使用

this.setAdapter(adapter); 

代替。

+0

该方法不存在ListActivity –

0

确保布局R.layout.shop有一个ListView元素,其ID为@android:id/list。见ListActivity

0

如果您使用的是ListActivity,在你的XML,列表视图应该像这样与android:id="@android:id/list"

<ListView android:id="@android:id/list" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:background="#00FF00" 
        android:layout_weight="1" 
        android:drawSelectorOnTop="false"/> 
+0

它的ID为“@android:id/list”(添加了xml) –

0

的问题是,由于某种原因的LinearLayout位于低在屏幕上,所以我不能看见。我将android:layout_marginTop="-500dp"添加到ListView并显示正确:)

+0

请看我建议的答案,它会帮助你更好的布局设计。 –